Creating Windows Batch Scripts
Updated: May 27
While the standard Windows command prompt (also known as cmd) is not the most powerful option available, it is still fairly full featured and has most of the scripting constructs you would expect.
To begin simply create a new file and give it a .bat extension. Open it in your favorite text editor and get to scripting.
Writing Output to the Screen
You can use the echo command to write output to the screen.
Note that you do not need to enclose the string Hello World! in single or double quotes. If you enclose the string in quotes they will be included in the terminal output.
One Windows oddity is that it echos each command to the terminal, even when run from within a batch script. In most cases this is not a desired behavior. To remedy the problem be sure to turn off echoing at the beginning of your batch script using the following line:
The at symbol (@) stops the command(s) that follow on the line from being echoed to the terminal.
Batch Script Comments
Comments in a batch script come in two varieties, by using the rem command, or by using double colon characters:
Let's get started with our first batch script, demo.bat:
Creating and Referencing Variables
Use the set command to store data in variables for later use. Below the value of variable MYVAR is set to the string Hello:
To reference the variable enclose the variable name in percent characters (%):
You can also mix variable references directly with string data, the cmd shell will automatically make the correct replacements:
Capturing User Input
You can use the set command with the /p parameter to obtain input from the user:
You can also use the pause command to temporarily stop script execution until the user hits a key.
Comparing with If Statements
If statements can be written on a single line or multiple lines. Here is a single line example that tests to see if the variable VAR is equal to 5, and if it is it will display VAR is 5 to the screen:
Here is the same example with the multi-line syntax, which allows you to include multiple commands within the if statement:
The Windows cmd shell also supports the else construct:
Here are the operators you can use when comparing values using if statements:
Controlling Program Flow with Goto
The goto command is one of the primary elements used to control program flow in batch scripts. As with other languages, goto allows you to jump to a specific location in your script defined by a label. The goto command can be used to jump forward or backward in the batch script.
In the example below the script will jump to the line of code labeled as Place1 if MYVAR is equal to 1, and it will jump to Place2 if MYVAR is equal to 2.
Note that when the batch script jumps to a label such as Place1, it will execute all of the code that follows until the end of the file, another jump occurs, or the script otherwise exits.
In batch scripts for loops can be used to iterate over numeric ranges and lists. Here is an example of looping from 0 to 10 (inclusive) with a step value of 1, the value of variable i will be printed to the screen during each iteration of the loop:
The /l parameter specifies that the for loop will iterate over a range.
You can also iterate over a list that contains any combination of numbers or letters:
Processing Script Parameters
Batch scripts can handle parameters just like any other command line program. To access a parameter within the script simply use the percent symbol [%] followed by the parameter's index. Indexes begin at 1.
Here is how param.bat looks when executed:
Special parameters also exist which can be referenced as follows:
%0 The name of the batch script as called
%* Returns all parameters
Batch scripts also support function-like constructs. Functions are defined using the same syntax as a goto label. In the example below the function is called Function1. You can use the call command to call the function. The exit command at the end of the function will exit the function and return processing back to where the call occurred. The /b option for exit allows you to specify a function return value. As is typical of command line programs, you should return 0 if the function succeeds, and a non-zero value if an error occurred.
In a batch script you can automatically take an action based on the success or failure of a command. For example, you can use double ampersands to specify an action to occur if the command succeeds:
Use the double pipe to specify an action to occur if the command fails:
To determine whether or not the last executed command failed or succeeded you can reference the special system variable %ERRORLEVEL%:
Batch Script Example
Here is a more complete example of a Windows batch script. It begins by looping over each of the parameters passed to the script. If a parameter is equal to Carl, it will print Carl is cool! to the screen, other wise it will print Hi <parameter> to the screen.
Here is the output from example.bat when executed with the parameters Dave, Carl, and Paul: