Introduction: Batch If Statements

Hi, Running a small server on Windows 7 64 Bit V1.0.0 Registered of Minecraft. I7 720 4.1Mhz CPU 12Gb Ram Nvidia GTX 570 - Super Overclocked 2 x. 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. When you get down to it, batch files and executable files work pretty much the same way. Both are simply a set of instructions and logic for Windows execute. So why would you want to convert a batch file to an executable if they essentially work the same? I am trying to set up multiple steam accounts, and you can instantly launch an account by making a shortcut for it, blah blah blah. The shortcuts works fine but I want to make a batch file to select which account to use, then launch the shortcut for that account. Batch Script - START - This batch command starts a program in new window, or opens a document.

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

Batch Script Start

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

Start Batch File Hidden

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' (
ECHO found
) 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)

Batch files call vs start

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
Set var=hello
Set var1=Hello
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
Pause
Goto eof
)
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 (
set 'var=%~2'
)
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
SET ONE=%~1
SET TWO=%~2
SET THREE=%~3
SET FOUR=%~4
SET FIVE=%~5
SET SIX=%~6
SET SEVEN=%~7
SET EIGHT=%~8
SET NINE=%~9
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
SET TEN=%~1
SET ELEVEN=%~2
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%.

Be the First to Share

Recommendations

Digital Measuring Roller Using Microbit & Tinkercad in Tools
Pocket Dice! Electronic Dice for Liars Dice and More in Electronics
50 7.0K
48 4.9K
  • Make it Glow Contest

  • First Time Author Contest

  • PCB Challenge

To start an exe file from a batch file in Windows, you can use the start command. For example, the following command would start Notepad in most versions of Windows.

START C:WindowsNOTEPAD.EXE

The start command can be used for other exe files by replacing the file path with the path to the exe file.

Tip

If you want to start multiple executable files, keep in mind that the batch file opens each of the files almost immediately. If you want some delay, consider using the pause command or sleep utility.

If the file path contains a space within a folder name, you need to enclose that folder name in double quotes. For example, if you had Google Chrome installed on your computer and wanted to start the Chrome browser through a batch file, you could use the command below.

START C:'Program Files (x86)'GoogleChromeApplicationchrome.exe

The Program Files (x86) folder name includes spaces in it. Enclosing the folder name in double quotes tells Windows the spaces are part of the folder name.

Can I control a program using a batch file once its been started?

No. A batch file can only execute or start another program. Once it is started, it cannot perform additional functions within that program.

BatchTip

In some situations, some programs may support additional syntax or options that allow you to perform additional functions.

Start Batch File

If you are looking for a language or tool to help perform more automation, we suggest AutoHotkey.

Batch Files Call Vs Start

Additional information

Start Batch File From Cmd

  • See our start command page for more information about this command.