More batch programming...
IMPORTANT NOTE: The evaluation of an errorlevel is true when
the current errorlevel is equal OR HIGHER than the number compared.
That means that, in:
IF ERRORLEVEL 3 GOTO :label
The condition is true
for errorlevel 3, 4, 5... and every errorlevel
equal or greater than
To clarify this,
read the next example.
ECHO 1.- Runs Windoze 3.11
ECHO 2.- Runs Dosshell
ECHO 3.- Runs Quake
ECHO X.- Exit program
CHOICE "Choose your option " /C:123x /N
IF ERRORLEVEL 4 GOTO end
IF ERRORLEVEL 3 c:\quake\quake -listen 16
IF ERRORLEVEL 2 dosshell
IF ERRORLEVEL 1 win
This is a complete
batch program. Lets analyze it.
First line turns ECHO
off: the command lines inside the program will not be shown.
Second line prints
an empty line (like pressing RETURN).
Lines 3 to 7 prints
the messages for the program.
Line 8 is choice.
This will show you only the text (because of the
/N flag) and will only allow you to press 1,2,3,4 or x key. It
unsensitive (no /S flag given). Note that the text is into quotes.
to make CHOICE respect the blank space at the end. Quotes will
not be shown, and are not required. If you unuse quotes, CHOICE
will not print the blank spaces of the begining or the end of
Lines 9 to 12 evaluates
the errorlevel. Note that:
Only numbers are evaluated. No X letter is writen.
It is in decreasing order. That is because the evaluation is
true if the current errorlevel is equal or higher. So
if I start with errorlevel 1, and errorlevel 4 (an
X) is pressed, I will execute the command anyway.
The execution of the batch file continues after the program
you called is runned. So, if the program returns
errorlevels, you may get an error. Commands CLS,
CD and DIR doesn't reset the current errorlevel, and
other DOS commands returns their own errorlevels. So, when you
call a DOS order, is better to make a GOTO anyway, to avoid stupid
errors. In the first IF ERRORLEVEL... the goto order does not
have : before the label name. This is because they are not required
in the goto call.
Line 13 is the label
end. When you call it, the programs finish.
4.- The command
Now we'll se how to
make a batch file smaller. The for order is not very useful,
but sometimes is exactly what you need. So lets see it.
The FOR command makes
a "variable" change it's value between the posibilities
you gave it, and executes a command every time it changes. The
sintax is that:
FOR %%A IN (list of values) DO command
Here %%A is the name
of the variable. (list of values) is just
the list of values (easy, uh?) between which will change the
variable %%A. The different values are separed by a blank space,
and are only considered as strings.
Programes NOTE: The variables are not true variables. They are
only valid in the FOR command, and will lose any meaning after
FOR is finished.
Lets see an example
to explain it:
FOR %%B IN (Hello Happy Hacker) DO ECHO %%B
This will make
the variable %%B change it's value. First time will
be Hello. Second time Happy, and third Hacker. Every time the
%%B value is changed, the command after the DO word (ECHO %%B
in our example) is executed. So this line in a batch file will
print in our screns:
Hello Happy Hacker
There is something
more to say about FOR. I have never tried it, but I read that:
when you give FOR a wildcard, it will take care to substitute
it for every existing filename that fits the wildcard. That is
that, if you have
FOR %%A in (AUTO*.BAT) DO copy %%A c:\backup
the copy command will recive every time complete file names and
no wildcards. This will execute, for example:
copy AUTOEXEC.BAT c:\backup
copy AUTODOS.BAT c:\backup
copy AUTODOS7.BAT c:\backup
And not something
copy AUTO*.BAT c:\backup.
This may be useful
with programs with no wildcards alowed, like the expand command,
that expand a compressed file.
5.- Other orders.
**REM and the commets.
To comment a complete
line, you should use REM at the begining of it. You can also
use : before tha line. But this will make the line a label, I
conseder this quite rough. It's up to you.
Newbie note: A comment is something ignored in the execution
of the program. Every programming language has at least one way
to make comments.
This is extremely
useful to make your program easier to read to other people and
to yourself in future.
When you call an .exe
of a .com or a DOS command inside a batch file, after the execution
of this program, the .bat file will continue at the point it
was before. But this is not this way when you call a .bat file.
I you run another .bat file inside another, the second one will
take control and won't return it after is finished. To run a
.bat file inside another and after continue the program that
called the second, we have to use the order CALL. That's what
CALL does: execute a .bat file and return to ours after it is
PAUSE is used to stop
the execution till a key is pressed. It gives
a message ("Pulse una tecla para continuar" in Spain),
and there is no way to change it. I you want to insert you own
message, the best way is to redirect the text of pause to NULL
(NULL is some kind'a black hole). This will make pause with no
message, and you can now insert yours:
ECHO This is my message. Press a key.
PAUSE > NULL
Another way to do
this is with the CHOICE order, but this limits
the valid keys to be pressed.
If you notice, you
can only use 9 parameters: from %1 to %9. In most of cases this
it more than enough. But, if you need to use more than 9 parameters
in your program, you can use the order SHIFT. SHIFT makes the
parameters lose their current value and will give them the value
of the next numbered parameters. Quite confusing, I know. Lets
If you give the order
SHIFT, %1 lose it's value and, from now, has second parameter
as content. The same with %2, that now refers to the third parameter...
and so on till the %9 parameter. Now, the %9 values the 10th
parameter, that was unaccesible before.
You can execute SHIFT
as many times as you want. But note that the maximun DOS order
width is 255 characters, so you don't make a program that needs
50 parameters because you won't be able to type all of them.
Well, that's all
I know about batch files. I hope you've enjoyed it
and excuse my spelling, my grammar... my English in general.
© 1998 firstname.lastname@example.org. You may forward or post this
GUIDE TO (mostly) HARMLESS HACKING on your Web site as long as
you leave this notice at the end.
More batch programming--->>