0

Currently I am writing a batch file to accept a parameter and jump to a specific part of the file based on the parameter value. The code I have is as follows:

SET param = %~n1

IF "%param%"=="q" GOTO :QUICK
IF "%param%"=="t" GOTO :THOROUGH
IF "%param%"=="b" GOTO :BOOT
IF "%param%"=="f" GOTO :FIX
IF "%param%"=="" GOTO :INVALID


:QUICK
echo "Quick"
pause
exit

:INVALID
echo "Invalid"
pause
exit

:BOOT
echo "Boot"
pause
exit

:FIX
echo "Fix"
pause
exit

:THOROUGH
echo "Thorough"
pause
exit

The only parameters the script should accept are /q, /t, /b or /f (or without /), and when an incorrect parameter is supplied, it should jump to INVALID. When I run it with an invalid parameter, it works, however, if I supply a parameter that is correct (for example, file.bat t) the result fails to work as I hope, and ends up going directly to INVALID. I have tried using "%param%" EQ "q", and setting q to a variable before doing the comparison, but I have had no luck coming up with a result that works.

1 Answer 1

3

you don't have a parameter %param%. But you have a parameter named %param<space>% And it's value isn't q, but <space>q

Write your setcommand without spaces:

set param=%~n1

or even better (to avoid unintended spaces at the end):

set "param=%~n1"

Note: you can make your parameters case independent with if /i "%param%"=="q"

Sign up to request clarification or add additional context in comments.

1 Comment

I didn't realize that the spaces between the variable and the equal sign were counted. It works perfectly now, thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.