Systems administrators and power users are well aware of the utility of batch files but the average PC user is generally unacquainted with them or is intimidated by the notion of writing or even running a script. This is unfortunate since it means that many are foregoing the use of a powerful tool for carrying out routine or repetitive tasks. Although batch files can be quite sophisticated and used for complicated network and system administration, they can also be of the utmost simplicity and very brief. In this article, I will introduce the batch file and discuss some uncomplicated examples that make basic tasks easier.
What is a batch file?
Constructing a batch file consists of nothing more than opening any text editor like the accessory Notepad, entering some lines containing commands, and saving the file with an extension BAT or CMD. (The CMD extension is limited to newer Windows systems and is not recognized in Windows 9x/Me systems. In Windows XP, Vista, and 7 there is little practical difference between the two extensions.) Don't use Wordpad or Word unless you are very careful to save all files in pure text format. The commands themselves are often quite simple and there is no need to learn a programming language. Those who wish can explore the intricacies that are available with branching and looping but here I will confine the discussion to some straightforward application to everyday tasks. The focus will be on saving time and effort for some routine stuff like system housekeeping and simple file management.
Running a batch file is a simple matter of clicking on it. Batch files can also be run in a command prompt or the Start-Run line. In that case, the full path name must be used unless the file's path is in the path environment.
Constructing a batch file
The first line in a batch file often consists of this command @echo off
By
default, a batch file will display its commands as it runs. The
purpose of this first command is to turn off this display. The command "echo
off" turns off the display for the whole script, except for the "echo
off"
command itself. The "at" sign "@" in front makes the command
apply to itself as well. This nuance isn't really all that important in the
context here but I mention it because it is often seen in scripts. The scripts
we will discuss are very brief and omitting
this line won't make any great difference. However, as a matter
of good practice, we will enter it in our scripts.
Our first batch file example is going to list all the files in a folder
and put the list in a new text file . We will use the directory command "dir" that
is discussed
on another page. Open Notepad and enter the line "@echo off" (without
quotes). Next enter another line dir
"C:\Program Files" > C:\list_of_program_files.txt
(I'm
assuming that your Program Files folder is on the C: drive). This will
give us the two-line file @echo off
dir
"C:\Program Files" > C:\list_of_program_files.txt
Now save
this two-line file as "listprograms.bat" (without quotes) to some
convenient location. Be sure that Notepad is saving as "All files" and
not as a text file. See the figure below.
Three important points are illustrated in the example script. Note that complete paths are used for files including the drive letter. Also note the quotes around "C:\Program Files". Paths must be quoted whenever a file or folder name has a space in it. Finally note the redirection symbol ">" that is used to send the output to a file instead of the screen.
All that has to be done to use the file is to double-click it. A file C:\list_of_program_files.txt will then be created.
A more general version with arguments
The file that we have been discussing is limited to listing one particular
folder and putting the list in one particular file. However, it is easy
to make the file able to list whatever folder we want and to put the
list wherever we want. Batch files can use arguments or data that is
input from the user. The process makes use of placeholders of the form
%1, %2, These are replaced in the script by our input data. This type
of situation cannot be clicked directly but should be run in a command
prompt. The new batch file would be@echo off
dir %1 >
%2
Enter
in Notepad and save as "makelist.bat". To run the file, open
a command prompt and enter{path}makelist somefolder somewhere\list.txt
where somefolder is
whatever folder (with complete path) that you want to list in somewhere\list.txt. Now
you have a little program that will list the contents of a folder whenever
you want. If you want a list of all the subfolders as well, use the command dir
/s %1 > %2
If you want a list that only includes files of a certain
type, MP3 files for example, use dir %1\*.mp3 > %2
The
line above illustrates the use of the wildcard "*". The ability
to use wildcards greatly enhances the power of batch files.
Life will be easier if you put all batch scripts in a folder that is in the path environment.
The Rem statement
Very often batch files contain lines that start with "Rem". This is a way to enter comments and documentation. The computer ignores anything on a line following Rem. For batch files of any complexity, comments are a good idea. Note that the command interpreter actually reads Rem statements so using too many can slow down execution of a script.More examples
Following the discussion on another page, it
is easy to create batch files for some typical maintenance. To create
a very simple backup script, use xcopy.
The code might be xcopy %1 %2 /d /s
This will update all files
in the input source folder %1 and its subfolders
by copying to the backup folder %2. In practice, a useful backup
script would probably need a few more of the switches discussed at xcopy.
Again following previous discussion of
the command "del", a file to delete all temporary files with extension
TMP might contain del %1\*.tmp
Prompting for user input
You can also interact with a user and ask that data be entered. The old DOS had a "Choice" command for very limited interaction but that has been superseded in Windows XP/Vista by the more versatile "set /p". The syntax is: set /p variable= [string]
"Variable" is the name of the variable that will be assigned to the data that you want the user to input. "String" is the message that the user will see as a prompt. If desired, "string" can be omitted. Here is an example that asks the user to enter his or her name: set /p name= What is your name?
This will create a variable %name% whose value is whatever the user enters. Note that the user must press the "Enter' key after typing the input.
(The "Choice" command has returned as a more powerful version in Vista.)
Further reading
These are simple examples and this page does not pretend to explain everything about batch files. The idea is to show how simple they are and to intrigue readers to look further into the subject. Even more powerful batch files can be constructed with the addition of simple decision making and methods of doing the same thing many times. Branching with "If" and "Goto" are discussed next ; using "For" to do repetitive tasks is considered on a third page.
Batch files are discussed in many books on Windows, at numerous Web sites and at this Microsoft site. Even if you do not want to write them, there are many already available for your use. This page at a sister site lists a number of sources.