CMD is misinterpreting code on the false side of an if
statement, resulting in a crash.
Here is some test code, which fails should the end user enter y
or Y
:
@Echo Off
Set "var="
Set "input="
:YorN
Set /P "input=Leave var empty? [Y(crash)|N]"
(Set input) 2>NUL | %SystemRoot%\System32\findstr.exe /I /L /X "input=Y input=N" 1>NUL
If ErrorLevel 1 GoTo YorN
If /I "%input%" == "n" Set "var=content1;content2;"
If Not "%var%" == "" (
For /F "Tokens=1,2 Delims=;" %%G In ("%var:~0,-1%") Do If Not "%%G" == "" Echo "%%G" "%%H"
) Else (
Echo As per your choosing, var is empty. Because of the if statement the "for" command didn't get interpreted and CMD didn't crash. You will not see this message.
)
Pause
Exit /B
This version however, with only one minor line break change works as intended.
@Echo Off
Set "var="
Set "input="
:YorN
Set /P "input=Leave var empty? [Y(crash)|N]"
(Set input) 2>NUL | %SystemRoot%\System32\findstr.exe /I /L /X "input=Y input=N" 1>NUL
If ErrorLevel 1 GoTo YorN
If /I "%input%" == "n" Set "var=content1;content2;"
If Not "%var%" == "" (
For /F "Tokens=1,2 Delims=;" %%G In ("%var:~0,-1%"
) Do If Not "%%G" == "" Echo "%%G" "%%H"
) Else (
Echo As per your choosing, var is empty. Because of the if statement the "for" command didn't get interpreted and CMD didn't crash. You will see this message.
)
Pause
Exit /B
Could somebody please explain to me what is causing this issue, or confirm that this is a bug in cmd.exe?