在Windows md-dos批处理文件中实现for循环或do while循环
我创建用户的代码如下:
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在每个部分之后,您可以将
goto exit
更改为goto menu
并在
:createuser
之前添加After each section, you could change
goto exit
togoto menu
and addright before
:createuser
for 循环由以下部分组成:
- 用于计算循环次数的变量
-实际内容
- 增加循环计数器变量的行
- 如果循环计数器超过某个值,则执行 GOTO 跳出循环的 if 语句
-A 转到循环开头
类似于
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
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.