There was a time when batch files ruled.
They were created by PC users who wanted to load up certain drivers at boot, or
to quickly rename a group of files, or perhaps to create a menu for the user to
choose whether they wish to run Windows 3.11, a game or just to drop into DOS
with loads of conventional RAM free. Whatever the reason, and whatever the
situation there were some of us who would automatically create a batch file to
fulfill a need.
Windows
3.11
Batch files these days are few and far
between, with most Windows experiences being handled by a nice looking GUI, but
there is still room for the humble batch file. And once you've started using
them, either for the first time, or just to remember how they were laid out,
you'll begin to wonder why you ever left them behind.
Over the next few issues we'll be looking
at some batch file basics, from outputting onto the screen, to storing
variables to creating menu, and maybe having a look at the odd game here and
there. Who ever said DOS was boring?
What Is A Batch File?
A batch file is simply a text file that
contains lines with commands that are executed in sequence, one after the other,
much like the old ZX Spectrum BASIC, for example. They have special extensions,
either BAT or CMD, and can be executed by simply typing in their name, as with
other system commands.
start
programs with batch file
You don't need any fancy compilers, just
Notepad will do - but not Word, WordPad or anything else like that, as they
contain special formatting that the batch file won't recognize. And one final
thing, you don't necessarily have to learn a programming language; there are
instances where a simple knowledge of how a program will work will aid you, but
generally speaking, as long as you know the syntax of a command, then you'll be
fine when creating a batch file.
Getting Started
First off, open Notepad and on the first
line type in:
@echo off
By default a batch file will display all
the commands that it runs through, line by line, what the @echo off command
does is turn that feature off for the whole script, with the '©' (at) sign to
apply that command to itself.
The next part of the script can be entirely
up to you, and what you want the batch file to achieve, but in this instance
we'll go through some basic examples and see where we end up.
Getting an Output
To keep things simple, let's add the
following command to the second line of the batch file:
dir "C:\Program Files\” >
c:\progs.txt
This line will grab the directory listing
of your C:\Program Files folder, and dump the contents into a text file called
progs.txt, in the root of C:\. Now add the following:
notepad.exe c:\progs.txt
This last command will now launch notepad
and display the contents of the progs.txt file you just created.
Set Command
While we are creating variables using the
set command, we have to be careful not to create a variable that will
ultimately wipe out the system specific variables. To find out what these
are, drop into a command prompt and type in:
set
This will display all the system
variables, along with any that you've since created. It's also a good place
to check if the system already has a variable in place to handle the current
user's name; which is already stored as %USERNAME%. So rather than asking for
the users' name in a batch file, you could simply start with:
@echo off
Echo Hello, %USEKNAME%, what are you
doing?
|