Batch file 避免意大利面代码
我一直在读怎么做 在“意大利面条代码”的示例中,我意识到登录时使用的批处理文件几乎符合此示例。有人能帮我使我的批处理文件没有意大利面代码吗Batch file 避免意大利面代码,batch-file,command-line,cmd,Batch File,Command Line,Cmd,我一直在读怎么做 在“意大利面条代码”的示例中,我意识到登录时使用的批处理文件几乎符合此示例。有人能帮我使我的批处理文件没有意大利面代码吗 @ECHO OFF CLS :MENU echo Welcome %USERNAME% echo 1 - Start KeePass echo 2 - Backup echo 3 - FireFox echo 4 - Exit SET /P M=Please Enter Selection, then Press Enter: IF %M%==1
@ECHO OFF
CLS
:MENU
echo Welcome %USERNAME%
echo 1 - Start KeePass
echo 2 - Backup
echo 3 - FireFox
echo 4 - Exit
SET /P M=Please Enter Selection, then Press Enter:
IF %M%==1 GOTO StarKeePass
IF %M%==2 GOTO Backup
IF %M%==3 GOTO FireFox
IF %M%==4 GOTO :EOF
GOTO MENU
: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
:FireFox
cd "C:\Program Files (x86)\Mozilla Firefox\"
start firefox.exe
GOTO MENU
在这种情况下,如果要使用子例程,应执行以下操作:
@ECHO OFF
CLS
:MENU
echo Welcome %USERNAME%
echo 1 - Start KeePass
echo 2 - Backup
echo 3 - FireFox
echo 4 - Exit
SET /P M=Please Enter Selection, then Press Enter:
IF %M%==1 CALL :StartKeePass
IF %M%==2 CALL :Backup
IF %M%==3 CALL :FireFox
IF %M%==4 GOTO :EOF
GOTO MENU
:StartKeePass
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 :EOF
:Backup
SET "backup=%USERPROFILE%\backup.bat"
call %backup%
GOTO :EOF
:FireFox
cd "C:\Program Files (x86)\Mozilla Firefox\"
start firefox.exe
GOTO :EOF
注意,我改变了一些事情。而不是去。。。转到菜单,您应该使用call:label
goto:eof
/退出/b
。除此之外,您还有一个拼写错误StartKeePass,与其使用set variable=“value”
,不如使用set“variable=value”
。这也将接受值中的空格,但不会向变量中添加引号
下次您可能应该将此发布到,因为在这种情况下,这些并不是真正的错误,如果您想使用子例程,您应该执行以下操作:
@ECHO OFF
CLS
:MENU
echo Welcome %USERNAME%
echo 1 - Start KeePass
echo 2 - Backup
echo 3 - FireFox
echo 4 - Exit
SET /P M=Please Enter Selection, then Press Enter:
IF %M%==1 CALL :StartKeePass
IF %M%==2 CALL :Backup
IF %M%==3 CALL :FireFox
IF %M%==4 GOTO :EOF
GOTO MENU
:StartKeePass
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 :EOF
:Backup
SET "backup=%USERPROFILE%\backup.bat"
call %backup%
GOTO :EOF
:FireFox
cd "C:\Program Files (x86)\Mozilla Firefox\"
start firefox.exe
GOTO :EOF
注意,我改变了一些事情。而不是去。。。转到菜单,您应该使用call:label
goto:eof
/退出/b
。除此之外,您还有一个拼写错误StartKeePass,与其使用set variable=“value”
,不如使用set“variable=value”
。这也将接受值中的空格,但不会向变量中添加引号
下次您可能应该将此发布到,因为这些都不是真正的错误如果您想完全删除
goto
s,您只需再次调用脚本即可继续使用它。此外,如果您使用的是XP之后的Windows版本,请查看该命令,因为它将消除检查用户是否输入了无效输入的需要
@echo off
cls
echo Welcome %USERNAME%
echo 1 - Start KeePass
echo 2 - Backup
echo 3 - FireFox
echo 4 - Exit
choice /C:1234 /M "Please enter your selection: " /N
:: The first option listed by choice's /C option will return an errorlevel value of 1, the second 2, and so on
if %errorlevel% equ 1 (
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%
)
:: I've converted these to one-liners simply for personal preference.
:: You can keep these the way you had them if you put them inside of parentheses like with option 1.
if %errorlevel% equ 2 call "%USERPROFILE%\backup.bat"
if %errorlevel% equ 3 start "" "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
if %errorlevel% equ 4 exit /b
:: Calls this script again, simulating a goto :MENU
:: Personally, I'd stick with a label and a goto in this instance,
:: but this is how you could do it if you don't want to use goto at all
call %0
如果用户可以做出的每个选择都相当简单(即可以简化为一个或两个命令),那么您可能希望以这种方式编码;否则,一定要使用Dennis建议的子例程。如果您想完全删除
goto
s,只需再次调用脚本即可继续使用它。此外,如果您使用的是XP之后的Windows版本,请查看该命令,因为它将消除检查用户是否输入了无效输入的需要
@echo off
cls
echo Welcome %USERNAME%
echo 1 - Start KeePass
echo 2 - Backup
echo 3 - FireFox
echo 4 - Exit
choice /C:1234 /M "Please enter your selection: " /N
:: The first option listed by choice's /C option will return an errorlevel value of 1, the second 2, and so on
if %errorlevel% equ 1 (
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%
)
:: I've converted these to one-liners simply for personal preference.
:: You can keep these the way you had them if you put them inside of parentheses like with option 1.
if %errorlevel% equ 2 call "%USERPROFILE%\backup.bat"
if %errorlevel% equ 3 start "" "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
if %errorlevel% equ 4 exit /b
:: Calls this script again, simulating a goto :MENU
:: Personally, I'd stick with a label and a goto in this instance,
:: but this is how you could do it if you don't want to use goto at all
call %0
如果用户可以做出的每个选择都相当简单(即可以简化为一个或两个命令),那么您可能希望以这种方式编码;否则,一定要像丹尼斯建议的那样使用子程序。我组织这项工作的方法是,在
m
变量中添加一个重置,允许处理一些意外输入,并在一个代码块中检查所有输入
“丹尼斯·范吉尔斯”的回答没有错,我想我会给你展示一种不同的方法
@echo off
setlocal enableDelayedExpansion
:menu
set "m="
cls
echo/Welcome !username!
echo/
echo/1 - Start keepass
echo/2 - Backup
echo/3 - Firefox
echo/4 - Exit
echo/
set /p "m=Please enter selection, then press enter:"
if not defined m (
cls
echo/Error: Empty input.
pause
) else (
if "!m!" equ "1" (
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!
) else (
if "!m!" equ "2" (
set "backup=!userprofile!\backup.bat"
call !backup!
) else (
if "!m!" equ "3" (
cd "c:\program files (x86)\mozilla firefox\"
start firefox.exe
) else (
if "!m!" equ "4" (
goto :eof
) else (
cls
echo/Error: ["!m!"] not recognized.
pause
)
)
)
)
)
goto :menu
注意:echo/
是一种习惯,因为echo:
和echo\
我误认为是文件路径/url的一部分,而echo.
由于其较长的命令时间而备受关注
另外,我更喜欢使用
代码>超过%
以及setlocal enableDelayedExpansion
(仅凭偏好)和易于分组编码。我组织这项工作的方法是,对m
变量进行重置,允许处理一些意外输入,并将其全部检查在一个代码块中
“丹尼斯·范吉尔斯”的回答没有错,我想我会给你展示一种不同的方法
@echo off
setlocal enableDelayedExpansion
:menu
set "m="
cls
echo/Welcome !username!
echo/
echo/1 - Start keepass
echo/2 - Backup
echo/3 - Firefox
echo/4 - Exit
echo/
set /p "m=Please enter selection, then press enter:"
if not defined m (
cls
echo/Error: Empty input.
pause
) else (
if "!m!" equ "1" (
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!
) else (
if "!m!" equ "2" (
set "backup=!userprofile!\backup.bat"
call !backup!
) else (
if "!m!" equ "3" (
cd "c:\program files (x86)\mozilla firefox\"
start firefox.exe
) else (
if "!m!" equ "4" (
goto :eof
) else (
cls
echo/Error: ["!m!"] not recognized.
pause
)
)
)
)
)
goto :menu
注意:echo/
是一种习惯,因为echo:
和echo\
我误认为是文件路径/url的一部分,而echo.
由于其较长的命令时间而备受关注
另外,我更喜欢使用代码>超过%
以及setlocal enableDelayedExpansion
,这完全不是意大利面条代码。例如,当函数调用在语法上嵌套得太深或其他构造(应该在逻辑上分组并拆分为多行)时,它被称为SC。@Ctx-感谢您的澄清。所以,从表面上看,这很好,对吗?我有另一篇关于如何使它更健壮的帖子,但这是一个不同的问题。在我看来很好,是的,这绝对不是意大利面条代码。例如,当函数调用在语法上嵌套得太深或其他构造(应该在逻辑上分组并拆分为多行)时,它被称为SC。@Ctx-感谢您的澄清。所以,从表面上看,这很好,对吗?我有另一篇关于如何使其更健壮的帖子,但这是一个不同的问题。我觉得很好,yesquick问题,为什么说goto:eof比goto:menu更好?为了解释它,call在其中创建了一个新的批处理文件,从给定的标签开始。当批处理文件完成后,它会返回到调用行下的调用文件。goto:eof并不是更好,但它可以选择使用子例程,或者只是在批处理文件中不断转到位置。我建议寻找一个更好的解释快速问题,为什么说goto:eof比说goto:menu更好?为了解释它,call在这个文件中创建一个新的批处理文件,从给定的标签开始。当批处理文件完成后,它会返回到调用行下的调用文件。goto:eof并不是更好,但它可以选择使用子例程,或者只是在批处理文件中不断转到位置。我建议找一个更好的解释