批处理文件来环路菜单 [英] Batch File To Loop Menu
问题描述
我创建一个批处理文件,当我登录到我的用户帐户将启动。我跟着本教程创建批处理文件的菜单。然而,它的工作原理,如果用户输入未列出一个数字,我希望它回到菜单。我将如何实现呢?
边注:我明白我可以使用一些更灵活的像PowerShell的,但是,我preFER批处理文件
下面是我到目前为止有:
@ECHO OFF
CLS
:菜单
呼应欢迎%USERNAME%回声1 - 开始的KeePass
回声2 - 备份
回声3 - 退出SET / P M = 1,2,3型则preSS输入:
IF%M%== 1 GOTO StarKeePass
IF%M%== 2 GOTO备份
IF%M%== 3 GOTO:EOF
:StarKeePass
SET的KeePass =%USERPROFILE%\\ KeePass的\\ KeePass的-2.30 \\ KeePass.exe
SET KDB =%USERPROFILE%\\ KeePass的\\ PasswordDatabase \\ PasswordDatabase.kdbx回声我会开始KeePass的适合你
开始→%的KeePass%%KDB%GOTO MENU:备份
设置备份=%USERPROFILE%\\ BACKUP.BAT
打电话%备份%GOTO MENU
要建立使用菜单设置/ P
,我建议以下变化:
- 所有的
如果
查询后,把转到:MENU
赶上意想不到的条目; - 重置的东西选择变量如
设置选择=
的设置/ P
命令之前,因为设置/ P
保持变量的前值的情况下,用户presses只是<大骨节病> ENTER 骨节病>; - 把报价
,
围绕在如果
语句前pressions避免的情况下,语法错误该变量为空或包含一些字符有特殊的含义(^
&安培;
(
)
&LT;
&GT;
|
;的字符还是引起问题虽然);
- 使用语法报价
设置[/ p]SELECT =什么
来避免特殊字符的麻烦; - 在大小写字母的查询,添加
/ I
开关置于如果
来强制区分大小写的比较; - 如果你愿意,你可以在
后,把
标签,让屏幕建成高达每次返回菜单;CLS
命令:MENU
下面是一个例子,演示了什么,我说什么:
关闭@echo:菜单
CLS
回声---主菜单---
回声1 - 做点什么
回音2 - 做别的事情
回声Q - 退出设置选择=
集/ PSELECT = 1型,2或Q&LT;输入计算值:
如果SELECT%%= =1GOTO:某处
如果SELECT%%==2GOTO:SOMEWHERE_ELSE
如果/ ISELECT%%==QGOTO:EOF
转到:MENU:某处
REM做一些事情在这里...
回声。
回声打印一些文字。
暂停
转到:MENU:别的地方
REM做别的事情在这里...
回声。
回声打印一些其他文本。
暂停
转到:MENU
要避免与字符上述烦恼,修改
设置/ P
块如下:
设置选择=
集/ PSELECT = 1型,2或Q&LT;输入计算值:
设置选择=%选择:=%
如果SELECT%%= =1GOTO:某处
如果SELECT%%==2GOTO:SOMEWHERE_ELSE
如果/ ISELECT%%==QGOTO:EOF
转到: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.
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:
- after all the
if
queries, put agoto :MENU
to catch unintended entries; - reset the selection variable by something like
set "SELECT="
before theset /P
command, becauseset /P
keep the former value of the variable in case the user presses just ENTER; - put quotes
""
around the expressions in theif
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); - use the quoted syntax
set [/P] "SELECT=anything"
to avoid trouble with special characters; - in case letters are queried, add the
/I
switch toif
to force case-insensitive comparison; - if you wish, you could put a
cls
command after the:MENU
label to let the screen be built up every time you return to the menu;
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 "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
这篇关于批处理文件来环路菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!