batch-file - 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.
解决方案
:: 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
使比较不区分大小写。用 处理的字符串比较==
。
用十进制等价物替换数字,003
即3
. 前导0
s 可以解释为八进制数。
双引号set
命令参数以避免可能的尾随空格。
选择exit
现在转到文件末尾 ( goto :eof
)。
启用显示s
helpme 。
推荐阅读
- python - 通过colab在google drive上保存word2vec模型
- c# - 不在 C# 中的 switch case 中输入
- python-3.x - 初学者 Python:帮助在连接到列表的函数中使用 while 循环
- angular - 如何使用 Angular 6 调试 dotnet core spa
- python - 导入一个python模块并实现它
- c++ - 背包问题,视觉工作室问题
- angular - 如何使用搜索操作在 Angular 中显示错误消息
- class - 如何将数据从一个类传递到另一个类,同时在 Flutter 中使其可变?
- angular - 订阅 ngrx 商店多次触发
- python - 如何将 int32 类型的 4D numpy 数组转换为 tfrecords?