21929 total geeks with 3247 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
redore
o noes the webserver was acting up earlier and it was spewing out all those full path disclosures ..
DnD
Tks, I'm checking my code.
CodeX
are you thinking of level 4? I'm sure there's been plenty of people who were dismayed after calculating the answer
MaxMouse
Also... Level5 lol, when you get the answer you'll shout at your computer, guaranteed.
MaxMouse
DnD, none of the challenges have been changed in a long time, i can assure you, none of them have a bug (Except level13, and it's more of a quirk than a bug)

Donate
Donate and help us fund new challenges
Donate!
Due Date: Feb 28
February Goal: $30.00
Gross: $0.00
Net Balance: $0.00
Left to go: $30.00
Contributors


News Feeds
The Register
Ex-Intel exec
pleads guilty to
insider trading
Adobe apologizes
for festering Flash
crash bug
Conficker outbreak
infects Leeds
hospital servers
Intel
"Tukwila" born
after long and
painful labor
SourceForge
reverses ban on US
foes
Oracle issues
emergency security
patch for WebLogic
Microsoft tests
show no Win 7
battery flaw
Microsoft kills
FAST"s Linux and
Unix search biz
Linus Torvalds
doesn"t hate the
Googlephone
Sweden to prosecute
alleged Cisco, NASA
hacker
Slashdot
Virtualizing a
Supercomputer
Study Says OOXML
Unsuitable For
Norwegian
Government
Virus-Detecting
"Lab On a Chip"
Developed At BYU
Google Shooting For
Smartphone
Universal
Translator
New Material
Transforms Car
Bodies Into
Batteries
Verizon Blocking
4chan
A Reflection On Sun
Executive Payouts
For Failure
Turns Out You
Actually Can Be
Bored To Death
Cacti 0.8 Network
Monitoring
What Are the Best
Valentine"s Day
Stunts?
Article viewer

Advanced Batch Programming - From the Basics to Recursivity



Written by:FoolFox
Published by:Nightscript
Published on:2005-11-17 00:09:46
Topic:Windows
Search OSI about Windows.More articles by FoolFox.
 viewed 35809 times send this article printer friendly

Digg this!
    Rate this article :
Well, in times where most of end-users face computers
through windowed systems, many of us may still need to get some
tasks done through a more basic interface . . .

You can find many introduction papers to the basics of batch
programming, but this time, we'll try to go a little bit beyond
that up to recursive batch programming. But, before digressing too much, here's a quick review of common commands that you can issue within a batch.

Commands

To get help on the format of commands, type <command> /? into your command windows:


  • ATTRIB--Change file attributes

    attrib +r+h test.txt


    The previous command sets read-only and hidden
    attributes on the file test.txt.

  • CLS--Clear the screen

  • EXIT--Close the command session. Self explanatory I think.

  • FOR--Initiate a loop. See below for a sample

  • FIND--Allows you to search for text in one or more files, allows you to search for specific files in memory, retrieving BIOS info. . .

  • GOTO--Branch flow to a known label

                    goto Step1
            echo this one is never printed
            :Step1
            echo this one is printed
            


  • IF--Allows for conditional statements

    if exist autoexec.bat echo Found!


    The previous command prints Found! on the screen
    if the file autoexec.bat is in the current dir.

  • MORE--Allows you to print output screen by screen

    type help.txt | more


  • MOVE--Move a file

  • SET--Define environment variable

                    set msg="Welcome "
            msg2=%msg%%username%
            echo msg2
             


    The previous batch sets one variable, creates a
    second one by concatenating the first variable with the username,
    and finally prints the result to the screen.

  • SHIFT--I'm pretty sure you were not as much aware of this one, a real nice trick, SHIFT allows you to move a parameter one step to left in a batch file.

  • START--Start the execution of a program in a new window. The user can select the type of window: maximized, minimized, or title, priority class for the running progam, or run it without any window. You can even WAIT for the process to terminate before executing your next batch instruction.

  • TYPE--Output file to default

    As you can see, most of those commands are in fact little
    executables that are run when requested by a batch file. Understand
    that you can issue ANY command or file name in a batch file, and
    it will be run. The usage of some of these commands will be explained
    later.

    Symbols that can be used

  • %--Using a % allows the use of variables inside a batch

  • @--Adding an at (@) in front of a command prevents the command from being echoed on the screen. For example, @DIR will execute DIR without showing the DIR command on one line.

  • >--Adding a > at the end of a command allows you to redirect the output. Overwriting the file in case of redirection to a file. Output can be a file result, text shown by the batch, etc. For example, DIR a: > test.txt will output content of dir into the file test.txt.

  • >>--Adding >> at the end of a command allows you to redirect
    the output. Appending in the case of redirection to a file.

                 @DIR a: > test.txt ;will output content of the dir in the file test.txt)

                 @DIR c: >> test.txt ;will append c: dir to previous file)


  • NUL - Null Device. Everything that is redirected to this
    devices as its output is sent to trash.

    @DIR c: /s > NUL (

                  will output dir of the whole HD (using /s) on
                                   NUL device. Lot of work, nothing is shown)


  • < - Adding a < at the end of a command allow to pass the
    part at the right of the "<" symbol (command, file,
    etc...) as parameter for the first command.

    ex :
    debug < file.txt (will send command in file.txt to the debugger)

  • | - Send output of the command to the input of another program

    ex :
    mem /c |find "MOUSE" (will execute mem /c and
                               send output to find.exe which
                                           will output only lines
                                           containing 'MOUSE')

    Advanced Batching:

    Now, let's play a little with basic commands we have seen before.

  • The FOR loop

    for %%f in (*.*) do echo There is a file called: %%f

    The previous command initiates a loop that searches for
    all file names in the current directory (*.*). All files
    found are then shown. Using this command you'll get a
    list of files found in current directory. Of course,
    you can be more specific in the location specification,
    or in the command. The following line will silently copy
    the content of %windir%repair to a floppy :

    for %f in (%windir%repair*.*) do @copy %f a:

    There is also a feature to use standard FOR loops
    with counters

    for /L %i in (1,1,5) do echo counter: %i

    The /L switch specifies that we are working with numbers.
    This loop will start at 1, use increment of 1, and will
    do the loop up to 5. Of course you can send all results
    to a file :

    for /L %i in (1,1,5) do echo counter: %i > result.txt

  • The IF statement

    The IF statement can be used to check if a condition
    is true or not. This statement can work with variables,
    or parameters sent to the batch:

        if [%1]==[/?] goto ShowHelp
        goto Continue
        :ShowHelp
        echo This is the help
        :Continue

    The IF statement can also be used in conjunction with
    the 'errorlevel' variable, a variable which handles the
    result of the last program executed.

        RunProg.exe
        if errorlevel 0 goto OK
        echo We have a problem!
        goto End
        :OK
        echo WOW! Everything went smooth!
        :End

  • SHIFTing parameters

    This can be useful when you have more than 10 parameters to pass to the batch file.
    Parameters are handled by batch files through escaped digits %0...%9. %0 contain the
    name of the batch file.

    If you have more than 9 parameters to go through, you can use the SHIFT command. This
    command will shift down each argument (%1 become %0, %2 become %1.... %9 gets a new
    param). For each SHIFT, %0 disappears. Once shifted out, an argument cannot be recovered.


  • Recursive batch files

    This one is really nifty. You can make recursive batch jobs using escaped digits. Let's take an example. I want to check all files in folder c:temp, if not existing in c:temp2
    then the batch have to copy the file, then dump content to the screen (type).

        echo off
        %1 %2
        for /F "tokens=1 delims= " %%i in ('dir /A-D /B c:temp*.*') do if not exist c:temp2%%i call %0 goto step1 %%i
        goto end
        :step1
        COPY c:temp%3 c:temp2%3
        type c:temp2%3
        echo -----------------------------------------------------------------------
        :end
        

    The point here to see is : call %0 goto step1 %%i within the DO command. We just ask the
    batch to call itself supplying goto and step1 as args %1 and %2. Now guess what the %1 %2
    line does....

    we could have done it in a much better way, like selecting in which case we have to perform the command :

        echo off
        if "%1" == "()" goto %2
        for /F "tokens=1 delims= " %%i in ('dir /A-D /B c:temp*.*') do if not exist c:temp2%%i call %0 () step1 %%i
        :step1
        COPY c:temp%3 c:temp2%3
        type c:temp2%3
        echo -----------------------------------------------------------------------
        :end
        

    This way you can add several conditions at startup.....

  • Creating ASM files using batch files:

    Yeah. Even creation of asm files is possible through a
    batch-job. Let's see the following batch :

        @echo off
        echo n prog.com > file1.txt
        echo a 0100 mov ah,09 >> file1.txt
        echo a 0102 mov dx,0109 >> file1.txt
        echo a 0105 int 21 >> file1.txt
        echo a 0107 int 20 >> file1.txt
        echo a 0109 db 'hello world!$ >> file1.txt
        echo rcx >> file1.txt
        echo 16 >> file1.txt
        echo w >> file1.txt
        echo q >> file1.txt
        debug < file1.txt
        del file1.txt
        prog

    At first, we are killing all echoing to the screen (echo off),
    this command itself is not shown.

    Then we create a file called file1.txt, of which the first line will
    be 'n prog.com'. This is a command for the debugger, defining
    the name of the executable to create.

    We then append several lines to the file file1.txt, which contain
    all assembly instruction to be compiled by the debugger (yeah,
    debug.exe, one of the fabulous tools you'll be able to find
    on EVERY windows installation, is also able to ASSEMBLE!!. I'm pretty sure
    you'll see those little dos exe in a different way now..).
    Current program will just print 'hello world' using INT 21h.

    Last lines include commands for the debugger (echo w = write
    file for the debugger, echo q = quit for the debugger, etc..)

    Then we compile our file ( debug < file1.txt ), delete the
    source and run the result.


  • How to close the console at the end of a batch job:

    You probably have already faced those command windows that stay
    wide open while the title say "Finished - xxxx". Instead of
    letting the user close the window himself, you can use the
    following command at the end of your batch:

    @cls

    When the batch is terminated, and the command screen remain
    blank, the system closes the window.

  • Using var as modifying code:

    We have seen that the use of the symbol % allow us to use
    a variable as a parameter. The fun thing here is that you can
    also use it as a command.

    Look at the following batch:

    @%sign%cls

    now, define sign as an empty variable (set sign= ). Run the batch; it
    will execute the command. Now if you define sign as a
    command (set sign=REM), you can run the batch the line
    will not be executed.

  • How to get a list of all .bat files on HD:

    Yeah, How to do so? Everybody knows the dir /s command, for
    sure, but there are more subtle ways to do it:

        @echo off
        chkdsk /v | find /i ".bat" > res.txt
        type res.txt | more
        del res.txt

    The /v option of chkdsk allows us to see each filename
    in every directory as the disk is checked. This list is
    piped through the find command, /i parameter is used to
    ignore case, and the %1 parameter specifies what we search
    for. Basically, everything. Warning, this will work when
    chkdsk output the file names. Won't work under NTFS.

    As Find will launch a new process each time it's called,
    we can't use it more efficiently. It won't work at all, in fact,
    each find command outputs one line to more, which print the
    line before returning to the loop. Using a temporary file
    to circumvent this.

    I hope having covered all what is needed to go a little beyond the
    basics, if you are interested in further inspecting this area, I strongly
    encourage you to turn your hear in the direction of batch virii, even if
    the domain is not ethical, they contain some nasty tricks that can help
    you learn a lot about what you are able to do with a batch file...

    FoolFox

  • Did you like this article? There are hundreds more.

    Comments:
    BloodHound
    2005-11-17 21:27:50
    I just wrote a batch script to compile assembly like.. 10 mins ago... I wish I checked here first. :(
    Good article though.
    Geek_Freek
    2005-11-18 04:55:27
    Well, i've written a Batch File Editor. Get it here : BAT-Man

    I ecspecially like the Message-Box thing.. looks so good ;)
    Geek_Freek
    2005-11-18 05:06:35
    Or if you want just the EXE
    BAT-Man
    telekinetic
    2005-11-19 03:53:21
    I found "Creating ASM files using batch files" pretty interesting. This lets you program even if the only thing you have is MS-DOS and nothing else !! Nice article.
    aidan
    2005-11-26 01:59:42
    here's a handy trick: && between commands will cause the seconf command to be executed only if the first returns sucessfully.

    I'm using bcc32 to compile stuff on windows at the moment. If I want the prog to run only if it compiles ok, I do this: bcc32 prog && prog

    Also works in *nix
    Arjuna
    2005-11-26 10:02:14
    just passing by comment:

    BAT files are only one of the two automation tools developed by Microsoft. The other one is Windows Scripting Host. It is a far more powerful language that allows access to ActiveX objects and is entirely object oriented.
    Quantris
    2005-11-26 22:57:31
    I don't really see the reason to use a batch file for the ASM stuff... wouldn't it be more straightforward to write the ASM into your text file directly and redirect it into debug from the command line?

    Anyway, this was a pretty interesting article and I learned a couple of new things! Good job!
    Anonymous
    2008-01-13 07:29:29
    I am trying to make a simple little game using a batch file. This requres me to be able to increase the value of a variable by one every time you get a point. In other words, the variable will start at 0, and whenever something happens, the value will go up by 1 or 2 etc...
    I can't for the life of me figure out how, if there's a different command from
    SET
    or if it's
    SET var+
    or something like that?
    Anyway, I would appreciate the help!
    Thanks
    Anonymous
    2008-01-23 18:00:03
    I don't know what you are really looking for in your batch file game, But I have sent information to a text file and called it back in for display.
    You should be able to keep score this way.
    Anonymous
    2008-02-04 17:51:15
    hey, how about

    set score=0
    :start
    set /a score=%score% + 1
    echo %score%
    if %score% LEQ 10 goto :start
    echo done !

    see
    help set
    Anonymous
    2009-09-17 20:15:50
    This is how you add in a Batch file:

    @Echo OFF
    Title Game
    ::Replace "Game" with the name of your game.
    set score=0
    set up=1
    set down=1

    :ScoreUp
    set /a score="%score%+%up%"
    echo %score%
    goto GAME
    :: Or any other Label

    :Score Down
    set /a score="%score%-%down%"
    echo %score%
    goto GAME
    :: Or any other Label

    ::Want to have a random number?
    set randomnum=%RANDOM:~1,1%
    ::This will set %randomnum% to a random 1 digit number.

    ::I've made several games in Batch. :)
    Anonymous
    2010-02-05 05:53:32
    http://www.legerdress.com/ herve leger dresses
    Anonymously add a comment: (or register here)
    (registration is really fast and we send you no spam)
    BB Code is enabled.
    Captcha Number:


    Blogs: (People who have posted blogs on this subject..)
    greengrub22
    Blog entry for Mon 24th Dec 11pm on Mon 24th Dec 11pm
    I am trying to make a batch file that will open the run menu. My problem is that I do not know the source for the run menu. I know this is probly something simple. Here is what I got... ........................................ ....... @echo off star
    bb
    SVN as windows service calling post-commit hanging as not asynchronous on Wed 19th Dec 1pm
    As any script you put inside post-commit.bat seems to be called synchronously, and doesnt inform the svn client that the commit has finished until the script has finished. I had to write a calling application which just starts the script in a new thread.
    shmad123
    Blog entry for Thu 1st Mar 6am on Thu 1st Mar 6am
    Hi my name is adam LOL

    Test Yourself: (why not try testing your skill on this subject? Clicking the link will start the test.)
    Microsoft by abhijangda

    Quiz based on the Microsoft Operating System
    Reverse Engineering basics by sefo

    I tried to cover the range of skills you will need to understand a win32 executable. Some of the following questions will take some time to answer. Do the test when you have enough free time.

    Related Links:
    New MS Shell Will Not Be In Longhorn
    sootman writes Remember that new Windows shell? Looks like itll be yet another technology that wont make it into Longhorn. It will take three to five years to fully develop and deliver, said Microsoft Senior Vice President Bob Muglia this week at Tec..
    Test Driving Linux
    Michael J. Ross writes As Windows users hear more about Linux, they may be intrigued to give it a try, if only to learn what the buzz is about. But a major hurdle, possibly the most daunting, is how to obtain and install Linux on their PCs without di..
    Microsoft preps critical Windows patch
    Next weeks security bulletin will deliver 10 fixes, at least one high-priority patch for Microsofts OS among them...
    Critical Windows patch coming from Microsoft
    Next weeks security bulletin will deliver 10 fixes, at least one high-priority patch for Microsofts OS among them...
    Windows to Have Better CLI
    MickyJ writes The command line interface to the Windows Server OS will be changed to the new Monad Shell (MSH), in a phased implementation to take place over the next three to five years. It will exceed what has been delivered in Linux and Unix for m..
    The ThinkPad X41 Tablet, which goes on sale early next week, is the first computer in the ThinkPad family to incorporate a version of Windows XP that is customized for many pen-based tablet functions...
    HOW TO: Convert a Mac into a X86
    inventgeek writes With the recent announcements Apple has made regarding its operating environment, Inventgeek.com has a mod that seems rather fitting. They have converted a Mac G3 to an Intel P4 System capable of running Windows or Linux. Full how t..
    Microsoft to Ship Modified Windows XP
    A version of the OS without Windows Media Player will be available in Europe next week...
    Is Intel a safe bet for Apple security?
    Macs have largely been immune to the viruses that plague Windows PCs. Experts pitch in on whether the Intel chip switch will change that...
    Microsoft Plans Hypervisor for Longhorn
    ninjee writes Microsoft reiterated plans to launch its own Windows-based hypervisor software for running multiple operating systems. Bob Muglia, senior vice president in the Windows Server Division, said on Tuesday that the software will be built dir..
    Steps to SEVERLY speed up the boot time of Vista
    [Bahasa Indonesia] Mendengarkan secara diam-diam Percakapan Telepon di Komputer
    Eavesdrop Telephone Conversations on Computer
    Basics To A Faster Computer
    Windows 2000 Administration 104
    Windows 2000 Administration 103
    Windows 2000 Administration 102
    Windows 2000 Administration 101
    Sharing in Windows
    Press CTRL + ALT + DEL To Login


         
    Your Ad Here
     
    Copyright Open Source Institute, 2006