在Windows md-dos批处理文件中实现for循环或do while循环

发布于 2024-10-06 11:29:33 字数 869 浏览 0 评论 0原文

我创建用户的代码如下:

@echo off
echo.
echo     SELECT OPTION
echo     -------------
echo.
:menu
echo     1- Create username
echo     2- Create password
echo     3- Delete username
echo     4- Exit
echo.
pause
set /p option=enter option 1 2 3 or 4:
if "%option%"=="1" goto createuser
if "%option%"=="2" goto createpass
if "%option%"=="3" goto deluser
if "%option%"=="4" goto exit

:createuser
echo Enter username
set /p var=Username:
net user %var% /add
echo Username successfully entered
goto exit

:createpass
echo Enter password
set /p var=Username:
net user %var% *
echo Password successfully entered
goto exit

:deluser
echo Enter user to be deleted
set /p var=Username:
net user %var% /del
echo User successfully deleted
goto exit

:exit

测试时它成功运行,但想知道如何成功实现 for 循环以查看用户是否输入数字 1-4 之外的任何选项(例如错误)。当成功添加用户时,如果他们想创建另一个用户或退出程序,还有一个 for 循环。

My coding for creating users is as follows:

@echo off
echo.
echo     SELECT OPTION
echo     -------------
echo.
:menu
echo     1- Create username
echo     2- Create password
echo     3- Delete username
echo     4- Exit
echo.
pause
set /p option=enter option 1 2 3 or 4:
if "%option%"=="1" goto createuser
if "%option%"=="2" goto createpass
if "%option%"=="3" goto deluser
if "%option%"=="4" goto exit

:createuser
echo Enter username
set /p var=Username:
net user %var% /add
echo Username successfully entered
goto exit

:createpass
echo Enter password
set /p var=Username:
net user %var% *
echo Password successfully entered
goto exit

:deluser
echo Enter user to be deleted
set /p var=Username:
net user %var% /del
echo User successfully deleted
goto exit

:exit

It successfully runs when tested but would like to know how to successfully implement a for loop to see if user enters any option outside of numbers 1-4 such as an error. Also a for loop for when a user has been successfully added, if they would like to create another user or exit the program.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜心 2024-10-13 11:29:34

在每个部分之后,您可以将 goto exit 更改为 goto menu

echo Error: Choose 1, 2, 3, or 4
goto menu

:createuser 之前添加

After each section, you could change goto exit to goto menu and add

echo Error: Choose 1, 2, 3, or 4
goto menu

right before :createuser

最美的太阳 2024-10-13 11:29:34

for 循环由以下部分组成:

- 用于计算循环次数的变量
-实际内容
- 增加循环计数器变量的行
- 如果循环计数器超过某个值,则执行 GOTO 跳出循环的 if 语句
-A 转到循环开头

类似于

set /p count = 0
:loopstart
doStuff
set count = count + 1
if %count%==10 GOTO loopend
GOTO loopstart

while 循环的东西是一样的,只不过不是计算循环计数器,而是替换一些其他条件,例如 if %option%=="quit" GOTO Loopend

希望您能根据此信息得到您想要的东西。抱歉,如果我的语法不完美,我不经常使用批处理文件。

A for loop consists of these parts:

-A variable for counting the number of loops
-Actual content
-A line that increments the loop counter variable
-An if statement that does a GOTO out of the loop if loop counter has exceeded some value
-A GOTO that goes to the beginning of the loop

Something like

set /p count = 0
:loopstart
doStuff
set count = count + 1
if %count%==10 GOTO loopend
GOTO loopstart

A while loop is the same thing, except instead of counting a loop counter, you substitute some other conditional such as if %option%=="quit" GOTO loopend

Hope you can get what you want based on this info. Sorry if my syntax isn't perfect, I don't use batch files very often.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文