Batch file programming is used to perform tasks from the command line. The script is run as an alternative to running each command by hand. Batch files have the Windows file extension .bat and can use any command line command, as well as a series of special keywords defined for command line programming.
The user needs only create a plain text file, with the .bat extension, edit it with a text editor, and call it from the Windows command line. Commands are executed sequentially, with some exceptions to be covered in a later article.
Some of the most common built-in Windows batch programming commands are:
The echo command is used in two ways. Firstly, it can be used at the start of a batch file program to turn off echoing to screen:
@echo off
The second way to use it is to display information to the user:
echo The Next Line Is Blank
echo.
If the programmer needs to stop the batch file for any reason then the pause command can be used to print a message to the user, and wait for a key to be pressed. The user can, at this point, press Ctrl+C to end the batch program. pause takes no other parameters.
To insert comments that are not displayed, but are useful to the programmer, the rem command can be used. Everything after it, up to the end of the line, is ignored when the command line interpreter 'runs' the batch script.
Finally, the shift command can be used to move the commands supplied on the command line down, thereby discarding the first, and allowing the script to access the next one without knowing how many have been supplied. For example:
@echo off
echo The first argument you supplied is : %1
shift
echo The second argument you supplied is : %1
This last example also shows how we can access parameters supplied on the command line : via the %parameter_number mechanism.
Any command that is accepted on the command line can be used in a batch file. Typically, the file handling commands such as the following are used:
We can also use the .exe filename of any application that we would like to run, so long as it can be found down the PATH variable, or the programmer supplies a fully qualified pathname.
The call command is useful in calling other batch programs. Unlike calling a command that is built into the command line interpreter, calling a batch file directly by name causes the parent to stop processing. Therefore, to permit both possibilities, the Windows command line programming language provides the call keyword to call a batch file. For example:
call mybatchfile %1 "literal"
In the above line of code, the mybatchfile.bat script is called with parameter 1 from the parent batch file, and a literal string.
Batch programming is very useful for performing repetitive tasks, such as compiling and linking programs, backing up data, or uploading files via FTP. Anything that has to be carried out on a routine basis via the command line can be written as a batch file script. The command line programming language is rich enough to support tasks that would also ordinarily be done via the GUI (such as Windows Explorer) with a little lateral thinking.