1

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.

michael_heath
  • 5,262
  • 2
  • 12
  • 22
user8385393
  • 104
  • 1
  • 9
  • 7
    You need to have all the `IF` comparisons use double quotes. – Squashman Nov 20 '18 at 19:52
  • 4
    The comparison operator `EQU` is designed primary for __integer__ comparisons and runs a string comparison only if one of the two compared strings cannot be successfully converted to an integer value. So it is better to use `==` which always makes a string comparison. See [Symbol equivalent to NEQ, LSS, GTR, etc. in Windows batch files](https://stackoverflow.com/a/47386323/3074564) for more details. – Mofi Nov 20 '18 at 20:20
  • 5
    I suggest to read [How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?](https://stackoverflow.com/a/49834019/3074564) It looks like a menu with __CHOICE__ would be better for this task and definitely much more secure and fault-tolerant than the prompt solution used by you which gives the user the freedom to enter really any string. [Debugging a batch file](https://stackoverflow.com/a/42448601/3074564) could be also of interest for you. – Mofi Nov 20 '18 at 20:24

1 Answers1

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

Minor fixes done mentioned below:

Double quoted string comparisons used with if. Used argument /i to make comparison case insensitive. String comparisons handled with ==.

Replaced numbers with decimal equivalents i.e. 003 to 3. Leading 0s can be interpreted as octal numbers.

Double quoted set command argument to avoid possible trailing spaces.

Selection exit now goes to the end of file (goto :eof).

Enabled s to show helpme.

michael_heath
  • 5,262
  • 2
  • 12
  • 22
  • You could minimise the lines by replacing, for example, `if /i "%input%" == "e" goto :eof` and `if /i "%input%" == "exit" goto :eof` with `If /I "%input:~,1%"=="e" GoTo :EOF`. – Compo Nov 20 '18 at 23:26
  • 1
    @Compo True, though anything starting with `e` could be interpreted as `exit` i.e. `elephant` would be accepted. – michael_heath Nov 21 '18 at 05:11
  • I did want to apologize for the messy code- because I was still working on making the code actually work, I wasn’t in the process of cleaning up the code I left behind. Thanks a bunch for this solution. – user8385393 Nov 21 '18 at 17:51