Introduction: Batch If Statements
If is one of the most important command in a batch file so I am making a tutorial devoted to the if command.
Step 1: If Statements
If statements are very useful and can be layer out in this form
If %variable% == "what variable should or can equal" [command]
You DO NOT have to use the goto command you can use any command so don't goto a lable just to echo text do this
If %var%==hello echo hi
To start something
If %var%==google start google.com
Step 2: /I Remove Repetition
If %input% == y goto y
If %input% ==Y goto y
If %input% == n goto n
If %input% == N goto n
This code is boring long and should be simplified I can split this code to half size with the /I parameter.
If /I %input% == y goto y
If /I %input% == n goto n
The /I parameter is not case sensitive so y and Y are interpreted as the same thing. In this case it isn't as big of a deal but in long codes it can make a difference.
Step 3: If Not and Exist
The id command can check if a file exists don't exist and the same thing for seeing if a variable is defined.
IF EXIST "file.ext" echo found
Remember to prevent batch files from getting lost when a file has spaces in the name add quotes. The line above checks to see if file.ext exists alternatively you can use
IF NOT EXIST "file.ext" echo lost :(
To tell you if file.ext doesn't exist or you can use ELSE to check both
IF EXIST "file.ext" (
) ELSE (
ECHO lost frown
Note: using :( may break the code by using a parenthesis.
Now was variable %var% defined already? This can be done two ways checking if the variable was defined or comparing %var% to "" (nothing) and seeing if they are equal.
IF "%var%"=="" (SET var=value)
Or you can check to see if the variable is defined
IF NOT DEFINED var (SET var=value)
Step 4: Comparisons
If statements use the following to determine if a number is _ than another number
EQU - equal
NEQ - not equal
LSS - less than
LEQ - less than or equal
GTR - greater than
GEQ - greater than or equal
So this can be used
IF (1) GEQ (5) echo "greater"
Alternatively the number may be encompassed by quotes.
Now how do we compare variables
If %var%==%var1% echo the same
This code sets the variables and then checks to see if they are the the same. The code will not be true because h and H aren't the same now switch that statement with
If /I %var%==%var1% echo the same
This will be true with the /I switch because it is no longer case sensitive.
Step 5: Parameters
Open cmd.exe and type color /? Or attrib +h nul.txt
Notice these commands have options /? For help and attrib allows for a file name if statements can let you do this
if /i "%~1"=="/?" (
Echo help section
This explained save it as help.bat open hold shift and right click in the same folder and open cmd type help /?
This code says if the first thing typed after the file name is /? Do what's in the parenthesis. Notice %~1 well what but the second thing I'm getting there.
if "%~2"=="" (
set /p var=Enter non-strict data
) else (
So you just change %~1 to %~2 yes, but what about the rest of the code?
Let's say you don't run it from cmd you open it like a batch file the second thing the user types into cmd is not valid so it prompts the user. But it looks different from code one why all those parenthesis? The first one required /? To go some where this allows for a file path to be typed which isn't the same text every time.
Step 6: More Than Nine Parameters
Batch can handle up to nine parameters so file.bat /1 /2 /3 /4 /5 /6 /7 /8 /9 will work but what if you want to user /10 or more use shift
So the first thing after the file name is set as %one% then use shift to reset parameters back to zero and restart so the tenth thing typed or shift shift shift ... %~1 is will take the tenth parameter ND set it to %ten%.