Batch Files (Scripts) in Windows
Batch files or scripts are small easy-to-write text files that carry out a series of commands. They can be simple enough that even the average home computer user can take advantage of them.

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?

These are simple text files containing some lines with commands that get executed in sequence, one after the other. These files have the special extension BAT or CMD. Files of this type are recognized and executed through an interface (sometimes called a shell) provided by a system file called the command interpreter. In Windows XP/ Vista the command interpreter is the file cmd.exe. The large assortment of versatile commands available in Windows XP/Vista/7 makes batch files a powerful tool.

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

In the following discussion it is assumed that the Introductory page and the page on Commands have been read.

The first line in a batch file often consists of this command @echo offBy 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 offdir "C:\Program Files" > C:\list_of_program_files.txtNow 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.

Saving a batch file with correct extension

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 offdir %1 > %2Enter in Notepad and save as "makelist.bat". To run the file, open a command prompt and enter{path}makelist somefolder somewhere\list.txtwhere 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.

Back to top Next: Branching and looping
{ezoic-ad-1}
{ez_footer_ads}