DOS Lesson 17: Batch File Variables; Nested Batch Files

Batch File Variables

In DOS Batch Files, you can create and use variables. The variables are signified by the use of the percentage symbol followed by a number. For
instance, look at the following batch file, which I will call test.bat:

@echo off
dir %1

This is a very simple batch file, but here is how you can use it. At the command prompt, type (I am assuming you are working in the C:\DOSTEMP\ working directory):

c:\dostemp\>test.bat c:\

If you have done this correctly, you should see a complete directory
listing of the root directory of C: What the batch file is doing is taking the
command DIR, and applying it to whatever directory you specify. If you neglect to specify a directory, you will get a listing of the current working directory (C:\DOSTEMP\), just as if you had issued a DIR command there without specifying the target directory. If you specify a directory on the command line, that directory is variable number 1, or %1. Now, suppose, you had issued the command:

c:\dostemp\>test.bat c:\ c:\windows\

If you try this, you will see that you will still get the listing of the root directory of C:, but only that. After all, your batch file is only looking for one variable, and besides the DIR command can only take one directory.

Also, you don’t really need to specify the extension of the batch file, unless you are unlucky enough to have picked a name for the batch file that matches one of the DOS external commands or something similar. When DOS executes “commands”, it goes in the following order:

  1. DOSKEY macros
  2. Internal Commands
  3. External Commands with the *.COM extension
  4. External Commands with the *.EXE extension
  5. Batch Files

So, as long you have never created a DOSKEY macro called “test”, you are safe using just the file name portion, since there is no DOS command, internal or external, called “test”.

OK, let’s rewrite our TEST.BAT file to use a command that takes two arguments, so we can see how this works. First, create a simple text file called TEST1.TXT, using the EDIT application. This is good practice anyway. Put a sentence of some kind inside (e.g. “Now is the time for all good men to come to the aid of their party.”), and save the file in your DOSTEMP working directory. Then, use EDIT to change your TEST.BAT file to the following:

@echo off
copy %1 %2

Save it, then execute the command:

c:\dostemp\>test test1.txt test2.txt

If you have done this correctly, you should see a directory listing of your
DOSTEMP working direcotry, and you should see three files listed: your TEST.BAT batch file, your TEST1.TXT text file, and a copy of the text file called TEST2.TXT, which was created by your batch file.

Nested Batch Files

Another feature of batch files is that they can be “nested”, which means that one batch file can be called and run inside of another batch file. To see how this works, let’s begin with a simple pair of batch files, which we will create in our DOSTEMP working directory. The first file, which we will call NBATCH1.BAT will be as follows:

@echo off
echo A 1
call nbatch2.bat
echo and a 3! Game over!

The second batch file, which as you have already guess is to be
called NBATCH2.BAT, will have the following contents:

@echo off
echo and a 2

Create both of these files using EDIT, and save them in your DOSTEMP
working directory. If you then run NBATCH1.BAT, you should see on your screen something like this:

A 1
and a 2
and a 3! Game over!

What happened here is the the NBATCH1.BAT file first “printed” on the screen, via the ECHO command, the phrase “A 1”. It then called the batch file NBATCH2.BAT, which in turn echoed to the screen the phrase “and a 2”, and then terminated and returned control to NBATCH1.BAT, which then echoed the final phrases “and a 3! Game over!” and terminated in turn, leaving you back at the prompt.

The command CALL is used in the first batch file to call the second batch file. You might wonder why this is needed, since you never need to use it for any other command, and batch files are just a collection of commands. Well, if NBATCH2.BAT was the last thing listed in the first batch file, you could probably get away with leaving out the CALL command. But to see what difference it makes, EDIT your NBATCH1.BAT file and remove the CALL command, so that your file now looks like:

@echo off
echo A 1
echo and a 3! Game over!

Save this edited version, and then run NBATCH1.BAT. You will now

A 1
and a 2

Aha! The second batch file terminated after it had finished running, but it did not return to the first batch file to let it finish running. Using the CALL command assures that control will always be passed back to the original
batch file, so you should always make it a practice to use the CALL command whenever you are nesting batch files, even if you are sometimes “chaining” one file after the other.

 Save as PDF