首页 > 解决方案 > Problems with inputs w/ spaces in a batch file

问题描述

I have a problem of an input being successful, but not being able to use the IF command with that input. I have already tried:

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

I have eliminated the possibility of an error in the :debug function, an error in the symbol '.', and the possibility of an error in the set /p input section. I am out of ideas on how the code is going wrong.

I know how to solve the common problem of spaces in batch, but for some reason, this code is still not working for me. Here is the code and the output from the debug file:

:: 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

At the end of DEBUG.TXT is where the batch window exited from a coding error.

标签: batch-fileinput

解决方案


:: 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. 前导0s 可以解释为八进制数。

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

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

启用显示shelpme 。


推荐阅读