coffeeismywater
coffeeismywater

Reputation: 11

Batch crashes with spaces even with var set and delayed expansion

I just started coding two days ago. (It's oddly addictive). And I started so that I could make a simulated "computer hacking" experience as a portion of an escape room that I am designing. Mainly, I have just been looking around and finding different ideas and putting them together in an aesthetically pleasing way. However, I want to make it crash resistant. So that when people are using it they don't get booted for just button mashing and pressing enter.

I got this to work in the :menu but not on the faux log in page. Below is the code that I wrote that I want to be able to work on other pages:

:menu 
cls 
set "answer=Nothing^!"
set var=%var: =%
echo. 
echo Menu
echo.
echo 1. Login
echo 2. Instructions
echo 3. Exit
echo.
set /p answer=Type the number of your option and press enter: 
echo.
echo.
if /I "!answer!" == "exit" goto exit
echo You answered: %answer%
echo Which is not a valid entry.
echo Try again.
if /I %answer% == 1 goto :Login1  
if /I %answer% == 2 goto :Instructions
if /I %answer% == 3 goto :Exit
if /I %answer% == 43153 goto :done 
pause
goto :menu

Here is the code I cannot get to work.

:Login2
cls
set "un=Nothing^!"
set "pw=Nothing^!"
set var=%var: =%
echo.
echo Log On
echo.
if %counter% lss 5 echo Password incorrect, %counter% attempts left
echo. 
echo. 
set /p un=Enter your UserID:
set /p pw=Enter your Password:
if /I %un% == ID (
if /I %pw% == PASSWORD goto :loading1 
)
if /I !un! == "exit" ( 
if !pw! == "exit" goto loading2
goto :loading2

If I type something random to input such as: asdf asdf The menu doesn't crash. But the login2 page does. It crashes if it is username and password OR just username OR just password that have spaces. And if I just "enter" through the screen it does not auto-populate " Nothing^! " like the menu does.

I would paste the entirety of the file below, but because with the decorative liberties I took it won't fit well on this page. And I can't find a place to upload it. But any comments or suggestions would be greatly appreciated.

Again, thank you for any help!

Upvotes: 1

Views: 42

Answers (2)

Magoo
Magoo

Reputation: 80033

if /I "!answer!" == "exit" goto exit

is correct

if /I %un% == ID (

is syntactically correct, provided un contains a simple string not containing spaces.

if un contains some spaces then the command is interpreted as

if /I some spaces == ID (

and the required syntax is

if [/i] string1 operator string2 dothis

An operator like == is also a string, so cmd sees spaces where it's expecting an operator, so it protests.

To allow spaces (and other separators) in a string, "quote the string" as you did with the first if above.

Oh - and it's particular about the quotes - they must exactly match - you can't quote one side and not the other.

This applies to string arguments only - where the arguments are numerics or resolve to a numeric, the quotes are not used. If they are used with numeric arguments, the arguments will be compared alphabetically so 98 is greater than 123 because 9 is greater than 1.

Upvotes: 2

jeb
jeb

Reputation: 82307

Always enquote variables in your IF expressions.

IF /i "%un%" == "ID" ...
IF /i "%pw%" == "PASSWORD" ...

or even better use delayed expansion here

IF /i "!un!" == "ID" ...
IF /i "!pw!" == "PASSWORD" ...

Upvotes: 1

Related Questions