Batch File Basics
I was recently asked by someone to review a windows batch file that they wrote. The author had very little experience with batch files and the first thing I noticed was that it could have been done a little better. Here are some of the batch file practices that may be new to some users that are just re-discovering the Command Prompt.
@echo off
@echo off should be the first line of any batch file that is being used by someone other than yourself. Actually it should be the first line of every batch file you write.
This tells the batch processor to not output the line of the batch file to the command window when running. If you specifically want to display some text you use the echo built in command to ouput to the console.
If you have a batch script that is quite long, echoing every line to the console can get very confusing.
Let’s take a simple batch file that has no @echo off
echo "Line 1"
echo "Line 2"
The output from that file is
c:\>echo "Line 1"
"Line 1"
c:\>echo "Line 2"
"Line 2"
Now adding @echo off to the beginning of the batch file
@echo off
echo "Line 1"
echo "Line 2"
Will output the following to the console
"Line 1"
"Line 2"
Always use @echo off as the first line of your batch file. If you need to debug, use @echo on or just add an echo statement at specific points of your script.
REM <> ::
Batch File Comments - more better
Don’t get me wrong, commenting a file is very good practice, but having 70 REM statements in a row is just not necessary. I was taught a long time ago to use REM sparingly as it took too long to process all the REM statements. Yes, this was back in the day when my PC had 4 MB of RAM!
There is a better way to comment. Using two colons is effectivly the same as a REM statement, but the comment is not processed by the batch processsor, so it’s faster. Not a big deal with todays computers, but why process something when you don’t have to.
When using REM, the entire line is processed by the batch processor and is seen as a comment, but using two colons ’::’ makes the batch processor stop reading the line and move on to the next line. This is because of how the batch processor treats a label. A label is used in a GOTO statement and defines where the goto is supposed to jump to. Here is an example of a GOTO statement and a label. Notice the single ‘:’ that defines the label.
@echo off
echo "The next line will jump to Label2"
Goto Label2
:Label1
echo "The batch file should never reach this line"
:Label2
echo "We have made it to Label2
When the batch processer sees two colons ’::’ it expects a label, but you can’t have a label named ’:’ so it just ignores the rest of the line. This is what makes the double colon a much better comment. If you run the following code, you will see it actually does not process the ‘::’ line, but it does process the REM line.
@echo on
:: This is a comment and is not processed by the batch processor
REM This is also a comment, but it is processed by the batch processor
Using the double colons is good if you have a single line comment, but if you have many lines of comments in a row you can skip the comment block by using a GOTO statement. Placed properly, the comment block is never processed.
@echo off
goto Start
Comment line 1
Comment line 2
Some more lines of comment
The batch processor should never touch these lines
:Start
echo %OS%
Math from the command prompt
You can do some math functions from the command line using SET /A. Normally SET, used just by itself will show you all the environment variables that have been configured. Using the /A switch allows for some arthmetic operations. This can be very handy when powershell scripts are not available for security reasons.
To do some simple math just use set /A expression
. This will output the answer to the console. To use the answer later in the batch file set the output to a variable.
::Output answer to console
set /A (3+1)*5
20
::Save output to variable
set /A ans1=(3+1)*5
::To use variable ans1
echo %ans1%
20
Using variables with SET /A does not require you to enclose the variable name in % signs. This is pretty handy when using multiple environment variable in a function. Using a variable name that has not been declared yet will substitute 0 for that variable name.
set /A ans1 + (20/5)
24
set /A ans2 = ans1 + ans2
20
set /A ans2 = ans1 + ans2
40