3. Variables and the Environment
The
use of variables in batch files can be somewhat confusing. All
variables used in a batch file (with the exception of command-line
parameters) are stored in the environment—an
area of memory that is created when you first boot and kept around
until the computer is turned off. The environment variable space is
discussed in more detail in the upcoming sidebar,
"The Path Less Travelled."
To view the contents of the environment, type set without any arguments. To set a variable to a particular value, type this command:
set VariableName=Some Data
Although
it isn't really emphasized as much as it was in the heyday of DOS and
Windows 3.x, the system path is still an important setting in Windows
Vista. It can be helpful as well as detrimental, depending on how it's
used. The system path is
simply a list of folder names kept in memory during an entire Windows
session. If a folder name is listed in your system path, you'll be able
to run a program contained in that folder without having to specify its location. The path is one of several environment variables
that are kept in memory from Windows startup until you shut down. In
early versions of Windows, the path was set with a line in the
now-obsolete Autoexec.bat file; now, all environment variables are set by going to Control Panel → System → Advanced System Settings → Environment Variables. By default, the Path system variable (shown in the lower box), contains the following folders: %SystemRoot%
%SystemRoot%\system32
%SystemRoot%\system32\Wbem
The %SystemRoot% element represents the Windows folder (usually C:\Windows). One of the consequences of this design is that if two different versions of the same executable file (any .exe, .dll, or .ocx
file, for instance) are placed in two different folders in the path,
only one of the available versions of the file—and not necessarily the
most recent one—may be in use at any given time. How
do you escape this trap? First, remove any unnecessary directories from
your path variable. Next, if you suspect a conflict with a specific
file, try searching your hard disk for the filename; if you see more
than one copy of the file in the search results, it could potentially
cause a conflict. Compare the versions of the files (right-click, select
Properties, and click the Details
tab), and delete (or temporarily rename, to be on the safe side) all
versions but the most recent. Then move the newest file to your \Windows\System32 folder if it's not already there. |
Unlike VBScript, the SET
command is required and no quotation marks are used when setting the
value of a variable. To remove the variable from memory, you set its
value to nothing, like this:
set VariableName=
To then display the contents of the variable, use the echo command, as follows:
echo %VariableName%
Here, the percent signs (%) on both ends of the variable name are mandatory; otherwise, the echo
command would take the argument literally and display the name of the
variable rather than the data it contains. What's confusing is that in
some cases, variables need no percent signs; sometimes they need one,
two at the beginning, or one on each end. More on this later.
4. Flow Control
Batch
files have a very rudimentary, but easy-to-understand flow-control
structure. The following example exhibits the use of the goto command:
@echo off
echo Griff
echo Asa
goto LaterOn
echo Ox
:LaterOn
echo Etch
The :LaterOn line (note the mandatory colon prefix) is called a label, which is used as a target for the goto command. If you follow the flow of the script, you should expect the following output:
Griff
Asa
Etch
because the goto command has caused the printing of Ox to be skipped. The label can appear before or after the goto line in a batch file, and you can have multiple goto commands and multiple labels.
5. Command-Line Parameters
Suppose you executed a batch file called Demo.bat by typing the following at the Command Prompt:
Demo file1.txt file2.txt
Both file1.txt and file2.txt are command-line parameters and are automatically stored in two variables, %1 and %2, respectively, when the batch file is run.
The
implication is that you could run a batch file that would then act with
the filenames or options that have been passed to it.
The following two-line example uses command-line parameters and the FC utility to compare two text files. A similar example using the Windows Script Host, takes 22 lines to accomplish approximately the same task:
fc %1 %2 >c:\windows\temp\output.txt
notepad c:\windows\temp\output.txt
Save this batch file as compare.bat, and execute it like this:
compare c:\windows\tips.txt c:\windows\faq.txt
which will compare the two files, tips.txt and faq.txt
(both located in your Windows folder), save the output to a temporary
file, and then display the output by opening the file in Notepad. Note
that the > character on the first line redirects the output of the FC program to the output.txt file, which would otherwise be displayed on the screen. The second line then opens the output.txt file in Notepad for easy viewing.
There are ways, other than typing, to take advantage of command-line parameters. If you place a shortcut to a batch file (say, Demo.bat) in your SendTo folder, then right-click on a file in Explorer, select Send To and then Demo, the Demo.bat
batch file will be executed with the file you've selected as the first
command-line parameter. Likewise, if you drag-drop any file onto the
batch-file icon in Explorer, the dropped file will be used as the
command-line parameter.
Batch files have a limit of nine command-line parameters (%1 through %9),
although there's a way to have more if you need them. Say you need to
accept 12 parameters at the command line; your batch file should start
by acting on the first parameter. Then, you would issue the shift command, which eliminates the first parameter, putting the second in its place. %2 becomes %1, %3 becomes %2, and so on. Just repeat the process until there are no parameters left. Here's an example of this process:
:StartOfLoop
if "%1"=="" exit
del %1
shift
goto StartOfLoop
Save these commands into MultiDel.bat. Now, this simple batch file deletes one or more filenames with a single command; it's used like this:
MultiDel file1.txt another.doc third.log
by cycling through the command-line parameters one by one using shift. It repeats the same two lines (del %1 and shift) until the %1 variable is empty (see "Section 9.5.6," next, for the use of the if statement), at which point the batch file ends (using the exit command).