[英]Batch File To Loop Menu
I'm creating a batch file that will launch when I login to my user account. 我正在创建一个批处理文件,该文件将在登录用户帐户时启动。 I followed this tutorial to create a batch file with a menu.
我按照本教程创建了带有菜单的批处理文件。 It works, however, if the user enters a number that is not listed, I want it to go back to the menu.
它有效,但是,如果用户输入的号码未列出,我希望它返回菜单。 How would I implement that?
我将如何实施?
Side note: I understand I could use something more flexible like Powershell, however, I prefer batch files. 旁注:我知道我可以使用Powershell之类的更灵活的工具,但是,我更喜欢批处理文件。
Here is what I have so far: 这是我到目前为止的内容:
@ECHO OFF
CLS
:MENU
echo Welcome %USERNAME%
echo 1 - Start KeePass
echo 2 - Backup
echo 3 - Exit
SET /P M=Type 1,2,3 then press Enter:
IF %M%==1 GOTO StarKeePass
IF %M%==2 GOTO Backup
IF %M%==3 GOTO :EOF
:StarKeePass
SET keePass="%USERPROFILE%\KeePass\KeePass-2.30\KeePass.exe"
SET kdb="%USERPROFILE%\KeePass\PasswordDatabase\PasswordDatabase.kdbx"
echo I'll start KeePass for You
START "" %keePass% %kdb%
GOTO MENU
:Backup
SET backup="%USERPROFILE%\backup.bat"
call %backup%
GOTO MENU
To build a menu using set /P
, I recommend the following changes: 要使用
set /P
构建菜单,我建议进行以下更改:
if
queries, put a goto :MENU
to catch unintended entries; if
查询之后,放入goto :MENU
捕获意外条目; set "SELECT="
before the set /P
command, because set /P
keep the former value of the variable in case the user presses just ENTER ; set /P
命令之前通过set "SELECT="
重置选择变量,因为set /P
保留变量的前一个值,以防用户仅按ENTER键 ; ""
around the expressions in the if
statements to avoid syntax errors in case the variable is empty or it contains some characters that have special meanings ( ^
&
(
)
<
>
|
; the "
character still causes problems though); ""
周围的表述中if
语句,以避免语法错误的情况下,该变量为空或包含一些字符有特殊的含义( ^
&
(
)
<
>
|
;在"
角色仍有问题虽然); set [/P] "SELECT=anything"
to avoid trouble with special characters; set [/P] "SELECT=anything"
来避免特殊字符的麻烦; /I
switch to if
to force case-insensitive comparison; /I
开关添加到if
以强制不区分大小写的比较; cls
command after the :MENU
label to let the screen be built up every time you return to the menu; :MENU
标签后面放置一个cls
命令,以便每次返回菜单时都可以建立屏幕。 Here is an example that demonstrates what I am talking about: 这是一个演示我在说什么的示例:
@echo off
:MENU
cls
echo --- MAIN MENU ---
echo 1 - do something
echo 2 - do something else
echo Q - quit
set "SELECT="
set /P "SELECT=Type 1, 2, or Q <Enter>: "
if "%SELECT%"=="1" GOTO :SOMEWHERE
if "%SELECT%"=="2" GOTO :SOMEWHERE_ELSE
if /I "%SELECT%"=="Q" GOTO :EOF
goto :MENU
:SOMEWHERE
rem do something here...
echo.
echo Print some text.
pause
goto :MENU
:SOMEWHERE_ELSE
rem do something else here...
echo.
echo Print some other text.
pause
goto :MENU
To avoid the above mentioned troubles with the "
character, modify the set /P
block as follows: 为避免上述带有
"
字符的麻烦,请如下修改set /P
块:
set "SELECT=""
set /P "SELECT=Type 1, 2, or Q <Enter>: "
set "SELECT=%SELECT:"=%"
if "%SELECT%"=="1" GOTO :SOMEWHERE
if "%SELECT%"=="2" GOTO :SOMEWHERE_ELSE
if /I "%SELECT%"=="Q" GOTO :EOF
goto :MENU
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.