- MCP server with stdio transport for local use - Search episodes, transcripts, hosts, and series - 4,511 episodes with metadata and transcripts - Data loader with in-memory JSON storage 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
135 lines
8.1 KiB
Plaintext
135 lines
8.1 KiB
Plaintext
Episode: 3740
|
|
Title: HPR3740: Batch File Variables; Nested Batch Files
|
|
Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr3740/hpr3740.mp3
|
|
Transcribed: 2025-10-25 04:49:23
|
|
|
|
---
|
|
|
|
This is Hacker Public Radio Episode 3,740 for Friday 2 December 2022.
|
|
Today's show is entitled, Batch File Variables Nested Batch Files.
|
|
It is part of the series' DOS.
|
|
It is hosted by AOKA, and is about 12 minutes long.
|
|
It carries a clean flag.
|
|
The summary is, more on DOS.
|
|
This time, it is using variables in Batch Files and Nesting Batch Files.
|
|
Hello, this is AOKA, welcoming you to Hacker Public Radio and another exciting episode
|
|
in our DOS series.
|
|
We are now coming to the end of our discussion of Batch Files, and we're also coming to
|
|
the end of this little series on DOS.
|
|
I wrote these 25, 30 years ago, and DOS was used a lot more than, but I hope that still
|
|
of some use, the feedback I've been getting is that a lot of people are enjoying these
|
|
particular episodes.
|
|
But you know, everything has to come to an end at some point.
|
|
Well, what I want to talk about today in completing our Batch File look is two things.
|
|
First of all, are using variables in Batch Files and Nested Batch Files.
|
|
So let's start with the 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, here is a Batch File, which I will call test.bat.
|
|
First line is at sign, echo, space, off.
|
|
Second line is dir, space, percent, number one.
|
|
That's a very simple Batch File, but here's how you can use it.
|
|
At the command prompt, type, and now I'm going to assume on all of this you're working
|
|
in your DOS temp, temporary directory, and use that as you're working directory.
|
|
So at the prompt, type test.bat, space, c, colon, backslash.
|
|
So if you've done this correctly, you should see a complete directory listing of the root
|
|
directory of c, colon, which is generally your hard drive.
|
|
What the Batch File is doing is taking the command, dir, and applying it to whatever directory
|
|
you specify.
|
|
Now if you neglect to specify a directory, you'll get a listing of the current working directory.
|
|
And again, I'm assuming right now that the current working directory is c, colon, backslash,
|
|
DOS temp, backslash.
|
|
So you would get a listing of that 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 one,
|
|
or percent one, as it is signified.
|
|
Now suppose you would issued the command at the prompt test.bat, space, c, colon, backslash,
|
|
space, c, colon, backslash, windows, backslash.
|
|
Now if you try this, you'll see that you will still get the listing of the root directory
|
|
of c, but that's all.
|
|
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 need to specify the extension of the Batch File.
|
|
I've been doing it as test.bat in all of these examples when you're typing the command.
|
|
You don't really need to do that unless you're 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 a specified order.
|
|
First it looks for DOS key macros, then internal commands, then external commands with the .com
|
|
extension.
|
|
So we had .com before there was .com.
|
|
External commands with the .exe extension, and then finally Batch Files.
|
|
So as long as you've never created a DOS key macro called test, you're safe just using
|
|
the file name test by itself since there is no DOS command, internal, or external called
|
|
test.
|
|
Anyway, back to the story here.
|
|
Let's rewrite our test.bat file to use a command that takes two arguments so we can
|
|
see how this works.
|
|
So first we'll create a simple text file called test1.txt using the edit application, just
|
|
for practice doing it.
|
|
Put a sentence of some kind inside, for instance, now is the time for all good men to come
|
|
to the aid of their party, and save that file in your DOS temp working directory.
|
|
Then use edit to change your test.bat file.
|
|
So now you've got test1.txt that has the sentence in it, now we're going to modify the
|
|
Batch file test.bat.
|
|
Now it's going to say, at echo space off on the first line, then copy space, percent
|
|
one space, percent two, that'll be your second line, and a third line, DIR.
|
|
Save it, and then execute the command, test space, test1.txt, space, test2.txt.
|
|
If you've done this correctly, you should see a directory listing of your DOS temp working
|
|
directory, and you should see three files listed, your test.bat.bat.file, your test1.txt
|
|
text file, and a copy of that text file called test2.txt, which was created by your batch
|
|
file.
|
|
Now let's take a look at nested batch files.
|
|
What I mean by nested is that one batch file can be called and run inside of another
|
|
batch file.
|
|
So let's examine how this works.
|
|
We'll begin with a simple pair of batch files, which we'll create in our DOS temp working
|
|
directory.
|
|
The first file, which we will call nbatch1.bat, will be as follows.
|
|
Next line, at sign echo space off, second line, echo space A, space 1, third line, call
|
|
space nbatch2.bat, fourth line, echo.
|
|
And space A, space 3, exclamation mark space, game space over exclamation mark.
|
|
Now the second batch file, and you've already guessed that it's going to be called nbatch2.bat,
|
|
because we had just put a call statement in the other one.
|
|
Now nbatch2 will have the following contents.
|
|
First line, at sign echo space off, second line, echo space, and space A, space 2.
|
|
So create both of those using the edit program and save them in your DOS temp working directory.
|
|
Now if you then run nbatch1.bat, you'll see something like this.
|
|
At the prompt, you'll type nbatch1.bat hit enter, and you will see on the next line, A, space
|
|
1, and then line under it, and space A, space 2, and then line under that, and space A, space
|
|
3, exclamation mark, space, game space over exclamation mark, and then you're prompt.
|
|
So running it is going to print out a 1, and a 2, and a 3 game over.
|
|
So what happens here is that the nbatch1.bat file first printed on the screen via the echo command,
|
|
the phrase a 1, and then it called the batch file nbatch2.bat, which in turn echoed to
|
|
the screen, the phrase, and the 2, and then terminated, and returned control to nbatch1,
|
|
which then echoed the final phrase, well phrases really, 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.
|
|
Now 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 nbatch1, you could
|
|
probably get away without using the call command, but to see what difference it makes, edit
|
|
your nbatch1.bat file and remove the call part.
|
|
So that your file now reads echo space a space1, next line nbatch2.bat, third line, echo space
|
|
and space a space, three exclamation mark space, game space, over exclamation mark.
|
|
So save this edited version, and now run nbatch1.
|
|
What do you get?
|
|
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're sometimes chaining one file after the other.
|
|
This comes under the heading of what we might call a best practice.
|
|
And so with that, this is a hookah for hacker public radio, winding up our look at DOS,
|
|
and encouraging everyone as always to support free software.
|
|
Bye bye.
|
|
You have been listening to Hacker Public Radio at HackerPublicRadio.org.
|
|
Today's show was contributed by a HPR listener like yourself.
|
|
If you ever thought of recording podcasts, click on our contribute link to find out how
|
|
easy it really is.
|
|
The hosting for HPR has been kindly provided by an honesthost.com, the internet archive
|
|
and our syncs.net.
|
|
On the Sadois status, today's show is released under Creative Commons, Attribution 4.0 International
|
|
License.
|