What's New!

Chat with

How to Defend
Your Computer 

The Guides
to (mostly) 
Harmless Hacking

Happy Hacker 
Digests (old stuff) 

Hacker Links 


Meet the 
Happy Hacksters 

Help for 



It Sucks 
to Be Me!

How to Commit
Computer Crime (not)! 

What Is a 
Hacker, Anyhow? 

Have a 
Great Life! 

News from the 
Hacker War Front

 Batch Programming


Microsoft-only version Number 3

Hacking with Win95/NT:  Batch File Programming

by Nezah

        We've learned what a batch file is and how to write them. Now it's
time to get all that batch files can bring us. Lets start.

        1.- The IF command
        It's easy to find out what it is for. The if command evaluates an
a condition and, in case of true result, it executes a command. There are
three ways to use IF. The different sintax are these:
        IF [NOT] EXIST file command
        IF [NOT] string1==string2 command
        IF [NOT] ERRORLEVEL number command
        Where command is the order (only one) you want to execute. The
NOT word is optional, and it makes the condition inverese. Lets see one
by one.

        **IF [NOT] EXIST file command.
        What it does is to find out if there is or not a file. If the file
exist (or not, if the NOT word is typed) then the command is executed.
Otherwise, the command is ignored.

TIP: Maybe you don't want to verify a file, but a drive or a directory. In
        this case, you have to look for the "file" NULL, that is present in
        any directory. For example, if you want to verify the c:\nezah
        directory, type this:
                IF EXIST C:\NEZAH\NULL command
        To test if a disquette is in, type this:
                IF EXIST A:\NULL command

        **IF [NOT] string1==string2 command
        Compares the two strings string1 and string2. In case that every
character is equal in both (case sensitive and blank spaces ignored), the
command is executed.
        It is useful to play with the parameters (read GTmHH Micro$oft 2).
Note now that, when a parameter call is found (for example, %1), DOS replaces
it for the text of the parameter, no matter where it is. For example, if %1
is Happy, when DOS finds:
        replaces it for:
        The same with   "%1"    --->    "Happy"
                        " %1 "  --->    " Happy "

NOTE: If a parameter does not exist, DOS replaces it with a blank space or
        just with nothing (in W95 OSR2 is nothing). To avoid lose control of
        the program, is useful to put every string into quotes. So, for example,
        if you want to see if a parameter is present, type:
                IF "%1"==" " command    --->    DOS 6.x
                IF "%1"=="" command     --->    In W95 is empty string ""
        In any other cases, quotes are not needed and strings are compared

        **IF [NOT] ERRORLEVEL number command
        This evaluates the last errorlevel number present. Errorlevels are
generated by programs to inform about the way they finished their execution.
For example, format returns errorlevel 3 when is Ctrl-C is pressed, and
errorlevel 0 when it finish normally. Not all the programs return errorlevels,
and errorlevels are lost when another program is runned.
        This is the most useful feature of IF command. We'll explain it later,
with the CHOICE command.

        2.- Labels and GOTO command.
        Labels are used to identify a line of the batch file. The way to
put a label inside a .bat progam is simply to put : before the label name.
For example, to create the "example" label, do this:
        And that's all.
        Labels get useful when you use the order GOTO. GOTO simply goes to
the labeled line you want. For example, to go to the :example label just type:
                GOTO :example
        That makes the execution of the .bat file continue above that line,
no matter if it was far below the current line or before it.

NOTE: The label does not defines a function (like in programming languajes).
        It makes the execution continue below the :label line.
        When, in normal execution, a :label line is found, it is ignored. The
        only use of lables is with the order GOTO.

TIP: You can create a :end label at the end of the file, so that, whenever
        you want to finish the program, you just have to type "GOTO :end".

TIP: Remember that, in the IF sentence, only was allowed one command.
        This sucks, I know. But now, you can create a label and make this
        only one command be GOTO :label. Then, in this :label you can have
        as many commands as you want, and finish the execution or return to
        the program point you wanted with :end or :new_label
More batch programming--->>

Carolyn's most
popular book,
in 4th edition now!
For advanced
hacker studies,
read Carolyn's
Google Groups
Subscribe to Happy Hacker
Visit this group


Return to the index of Guides to (mostly) Harmless Hacking!

 © 2013 Happy Hacker All rights reserved.