实现一个for循环或while循环在windows MD-DOS批处理文件做 [英] Implement a for loop or do while loop in windows md-dos batch file

查看:5984
本文介绍了实现一个for循环或while循环在windows MD-DOS批处理文件做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建用户编码如下:

@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

据测试成功,但想知道如何成功地实现一个循环,看看用户数1-4的外部进入的任何选项,如错误时运行。另外一个for循环,当用户已经成功添加,如果他们想创造另一个用户或退出程序。

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.

推荐答案

一个for循环由以下几部分组成:

A for loop consists of these parts:

计数循环结果的数量-A变量
- 实际内容的结果
-A线递增循环计数器变量结果
-an如果这样做一个GOTO跳出循环语句,如果循环计数器已经超过某个值结果
-A GOTO该进到循环的开始

-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

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

一个while循环是一样的东西,除了没有计数循环计数器,你代替其他一些条件,如如果选项%==%跳槽GOTO loopend

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.

这篇关于实现一个for循环或while循环在windows MD-DOS批处理文件做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆