The command line is
still an often-useful and occasionally indispensable part of computing
life, and most power users will find themselves doing at least a little
work in the Command Prompt window. Part of that work might involve
writing short batch file programs to automate routine chores, such as
performing simple file backups and deleting unneeded files. And if you
throw in any of the commands that enhance batch files, you can do many
other interesting and useful things.
When you run a command in a command-line session, the
command prompt executes the command or program and returns to the
prompt to await further orders. If you tell the command prompt to
execute a batch file, however, things are a little different. The
command prompt goes into Batch mode,
where it takes all its input from the individual lines of a batch file.
These lines are just commands that (in most cases) you otherwise have to
type in yourself. The command prompt repeats the following four-step
procedure until it has processed each line in the batch file:
1. | It reads a line from the batch file.
|
2. | It closes the batch file.
|
3. | It executes the command.
|
4. | It reopens the batch file and reads the next line.
|
The main advantage of Batch mode is that you can lump
several commands together in a single batch file and tell the command
prompt to execute them all simply by typing the name of the batch file. This is great for automating routine tasks such as backing up the Registry files or deleting leftover .tmp files at startup.
Creating Batch Files
Before getting started with some concrete batch file
examples, you need to know how to create them. Here are a few things to
bear in mind:
Batch files are simple text files, so using Notepad (or some other text editor) is probably your best choice.
If you decide to use WordPad or another word processor, make sure that the file you create is a text-only file.
Save your batch files using the .bat extension.
When
naming your batch files, don’t use the same name as a command prompt
command. For example, if you create a batch file that deletes some
files, don’t name it Del.bat. If you do, the batch file will never run! Here’s why: When you enter something at the prompt, CMD first checks to see whether the command is an internal command. If it’s not, CMD then checks for (in order) a .com, .exe, .bat, or .cmd file with a matching name. Because all external commands use a .com or .exe extension, CMD never bothers to check whether your batch file even exists!
After you’ve created the batch file, the rest is
easy. Just enter any commands exactly as you would at the command line,
and include whatever batch instructions you need.
Tip
If you find yourself creating and using a number of
batch files, things can get confusing if you have the files scattered
all over your hard disk. To remedy this, it makes sense to create a new
folder to hold all your batch files. To make this strategy effective,
however, you have to tell the command prompt to look in the batch file
folder to find these files. To do that, you need to add the batch file
folder to the PATH variable.
REM: Adding Comments to a Batch File
The first of the batch file–specific commands is REM (which stands for remark).
This simple command tells the command prompt to ignore everything else
on the current line. Batch file mavens use it almost exclusively to add
short comments to their files:
REM This batch file changes to drive C
REM folder and starts CHKDSK in automatic mode.
C:
CHKDSK /F
Caution
It’s best not to go overboard with REM statements. Having too many slows a batch file to a crawl. You really need only a few REM statements at the beginning to outline the purpose of the file and one or two to explain each of your more cryptic commands.
ECHO: Displaying Messages from a Batch File
When it’s processing a batch file, Windows Home
Server normally lets you know what’s going on by displaying each command
before executing it. That’s fine, but it’s often better to include more
expansive descriptions, especially if other people will be using your
batch files. The ECHO batch file command makes it possible for you to do just that.
For example, here’s a simple batch file that deletes all the text files in the current user’s Cookies and Recent folders and courteously tells the user what’s about to happen:
ECHO This batch file will now delete all your cookie text files
DEL "%LocalAppData%\Microsoft\Windows\Temporary Internet Files\cookie*"
ECHO This batch file will now delete your Recent Items list
DEL "%AppData%\Microsoft\Windows\Recent Items\*.lnk"
The idea here is that when Windows Home Server stumbles on the ECHO
command, it simply displays the rest of the line onscreen. Sounds
pretty simple, right? Well, here’s what the output looks like when you
run the batch file:
C:\>ECHO This batch file will now delete all your cookie text files
This batch file will now delete all your cookie text files
C:\>DEL "%LocalAppData%\Microsoft\Windows\Temporary Internet Files\cookie*""
C:\>ECHO This batch file will now delete your Recent Items list
This batch file will now delete your Recent Items list
C:\>DEL "%AppData%\Microsoft\Windows\Recent Items\*.lnk"
What a mess! The problem is that Windows Home Server is displaying the command and ECHOing the line. Fortunately, Windows Home Server provides two solutions:
To prevent Windows Home Server from displaying a command as it executes, precede the command with the @ symbol:
@ECHO This batch file will now delete all your cookie text files
To prevent Windows Home Server from displaying commands, place the following at the beginning of the batch file:
Here’s what the output looks like with the commands hidden:
This batch file will now delete all your cookie text files
This batch file will now delete your Recent Items list
Tip
You might think that you can display a blank line simply by using ECHO by itself. That would be nice, but it doesn’t work. (Windows Home Server just tells you the current state of ECHO: on or off.) Instead, use ECHO. (ECHO followed by a period).
PAUSE: Temporarily Halting Batch File Execution
Sometimes you want to see something that a batch file displays (such as a folder listing produced by the DIR
command) before continuing. Or, you might want to alert users that
something important is about to happen so that they can consider the
possible ramifications (and bail out if they get cold feet). In both
cases, you can use the PAUSE command to halt the execution of a batch file temporarily. When Windows Home Server comes across PAUSE in a batch file, it displays the following:
Press any key to continue . . .
To continue processing the rest of the batch file,
press any key. If you don’t want to continue, you can cancel processing
by pressing Ctrl+C or Ctrl+Break. Windows Home Server then asks you to
confirm:
Terminate batch job (Y/N)?
Either press Y to return to the prompt or N to continue the batch file.