批处理文件中带有空格的输入问题

时间:2018-11-20 19:47:14

标签: batch-file input

我有一个输入成功的问题,但无法对该输入使用IF命令。我已经尝试过:

if "%input%" equ "s helpme" goto localhost.mail.helpme

我消除了:debug function中出现错误的可能性,符号'.'中出现错误的可能性以及set /p输入部分中出错的可能性。我对代码的错误方式一无所知。

我知道如何批量解决空格的常见问题,但是由于某些原因,此代码仍不适用于我。这是代码和调试文件的输出:

:: GAME.BAT

set lvl=003
set debug=true

:localhost.mail
call :debug "vdir localhost.mail"
if %lvl% equ 002 goto localhost.mail.help
set /p input=localhost.mail 
call :debug "localhost.mail input %input%"
if %input% equ h goto localhost.mail.help
if %input% equ help goto localhost.mail.help
if %input% equ listmail goto localhost.mail.list
if %input% equ l goto localhost.mail.list
if "%input%" equ "s helpme" goto localhost.mail.helpme
if "%input%" equ "show helpme" goto localhost.mail.helpme
if %input% equ e goto localhost
if %input% equ exit goto localhost

:localhost.mail.help
call :debug "vdir localhost.mail.help"
if %lvl% equ 002 echo Advanced to level 3
if %lvl% equ 002 set lvl=003
echo (h)elp - Display this help screen
echo (l)istmail - Lists emails
echo (s)how - Shows an email ex. show helpme
echo (e)xit - Exits email
goto localhost.mail

:localhost.mail.list
call :debug "vdir localhost.mail.list"
if %lvl% gtr 002 echo helpme
goto localhost.mail

:localhost.mail.helpme
call :debug "vdir localhost.mail.helpme"
if %lvl% lss 003 goto localhost.mail
echo WORK IN PROGRESS, COME BACK NEXT UPDATE!
echo.
goto localhost.mail

:debug
if %debug% equ true echo %date% %time% %~1>>debug.txt
GOTO :EOF

:: DEBUG.TXT
Tue 11/20/2018 13:33:05.84 vdir localhost.mail
Tue 11/20/2018 13:33:05.86 vdir localhost.mail.help
Tue 11/20/2018 13:33:05.87 vdir localhost.mail
Tue 11/20/2018 13:33:10.15 localhost.mail input s helpme

DEBUG.TXT的末尾是批处理窗口因编码错误而退出的地方。

1 个答案:

答案 0 :(得分:1)

:: GAME.BAT
set "lvl=3"
set "debug=true"

:localhost.mail
call :debug "vdir localhost.mail"
if %lvl% equ 2 goto localhost.mail.help
set /p "input=localhost.mail: "
call :debug "localhost.mail input %input%"
if /i "%input%" == "h" goto localhost.mail.help
if /i "%input%" == "help" goto localhost.mail.help
if /i "%input%" == "l" goto localhost.mail.list
if /i "%input%" == "listmail" goto localhost.mail.list
if /i "%input%" == "s" goto localhost.mail.helpme
if /i "%input%" == "show helpme" goto localhost.mail.helpme
if /i "%input%" == "e" goto :eof
if /i "%input%" == "exit" goto :eof
goto localhost.mail

:localhost.mail.help
call :debug "vdir localhost.mail.help"
if %lvl% equ 2 echo Advanced to level 3
if %lvl% equ 2 set "lvl=3"
echo (h)elp - Display this help screen
echo (l)istmail - Lists emails
echo (s)how - Shows an email ex. show helpme
echo (e)xit - Exits email
goto localhost.mail

:localhost.mail.list
call :debug "vdir localhost.mail.list"
if %lvl% gtr 2 echo helpme
goto localhost.mail

:localhost.mail.helpme
call :debug "vdir localhost.mail.helpme"
if %lvl% lss 3 goto localhost.mail
echo WORK IN PROGRESS, COME BACK NEXT UPDATE!
echo.
goto localhost.mail

:debug
if /i "%debug%" == "true" >> debug.txt echo %date% %time% %~1
goto :eof

下面提到的一些较小的修复:

if一起使用的双引号字符串比较。使用参数/i使比较不区分大小写。用==处理的字符串比较。

用等价的十进制替换数字,即0033。前导0可以解释为八进制数字。

使用双引号set命令参数以避免可能的尾随空格。

选择exit现在转到文件(goto :eof)的末尾。

已启用s来显示帮助文件。