Results 1 to 4 of 4

Thread: Newbies Guide to Batch Programming

  1. #1

    Newbies Guide to Batch Programming

    Hey, before I start this tutorial let me get one thing straight-I am not a batch master, do NOT e-mail me asking complicated questions about batch commands and so on. I know basic batch, and thats all that I am writing in this tutorial, so do not send or ask any complicated questions.

    What is Batch?
    Batch is a DOS language developed by IBM. It uses MS-DOS commands and executes all of them in a single file. Basically, it types many DOS commands by itself, saving you time (and arthritis). I am writing this tutorial assuming that the reader knows at least the bare essentials of MS-DOS, such as the commands cd, dir, mkdir, del, etc. If you do not know any DOS commands or not enough to read this, i suggest you learn them first, knowing the commands will help you understand this tutorial ALOT. You can look below for some of the commands, but I might forget a few.

    How do I write batch?
    You can write batch on most simple text editors. The most common text editor for writing batch is Notepad, but you can also use Wordpad and MS-DOS edit (to use DOS edit simply go to the directory you want your text file in and type edit (filename) example: {C:\edit hello} this would make a text file called Hello. of course the brackets wouldnt be there, i just put them there so you could tell what the example was)

    How do I execute a batch file?
    To execute a batch file in Windows you can simply double click on it. However, the file will close automatically, and if you want to see the code in the file you should use DOS to execute it. To execute one in DOS simply change to the directory that the file is in and type batchfilename.bat and it will automatically start the file.

    Basic DOS Commands
    cd -changes the current directory
    dir -lists unhidden files in the current directory
    mkdir -makes a new directory
    rmdir -removes a directory
    del -removes a file (cannot be used to remove directories)
    rem -puts a remark in the file. anything on the line with rem will be ignored. can be very useful for inserting comments.
    echo -prints text onto the screen

    Let's do this!
    Ok first we will start with a very basic batch file. Open up a basic text editor and type
    cd C:\Progra~1
    mkdir Hello

    save the file as filename.bat
    NOTE: Make sure you save the file extension as .bat or else DOS cannot read the batch file!
    Now lets execute the file- C:\Directory: filename.bat
    on the screen it will show
    C:\directory>cd C:\Progra~1
    C:\Progra~1>mkdir Hello

    This batch file changes the directory to C:\Program Files and makes the directory Hello. NOTE-If your wondering why i typed cd C:\Progra~1 and not cd C:\Program Files, this is because DOS will only read the first section of the directory name. If the directory has a space in it you must type the first 6 letters of the directory name and after that you type ~1.

    Now lets say you want to insert comments into the file. To do this simply type
    REM this is a remark and it will be ignored by DOS
    rem can be very helpful for inserting facts about the code, such as REM This is a batch file designed to delete temporary internet files

    What if you don't want people to see your code? To do this type Echo Off before the code you don't want people to see. To turn Echo back on type Echo On . If you want to hide only one piece of code you insert a @ at the beginning (example: @cd C:\Windows) This will hide the code when the batch file is executed.

    Variables are needed to make batch files smart. Variables are defined as %1 through %9. These variables are typed after the batch file name when you execute it. (example: C:\Batchf~1>filename.bat jack) In this case the variable would be jack. Lets tell the batch program to make a user defined directory.
    mkdir %1
    The batch program would then make the directory that the user named to make.
    Variables are very helpful and can be put to better purposes than I have shown here.

    The IF Command
    IF is a very helpful batch command. It checks a user-defined statement, and then issues a user-defined command. Here is an example
    IF EXIST C:\Windows\hello.txt DEL hello.txt
    In this case the batch checks to see if the file hello.txt, located in C:\Windows, exists, and if it does it deletes hello.txt
    EXIST is probably the most commonly used IF argument. You can also use IF NOT EXIST, which checks to see if the file does not exist.
    Checking to see if a folder exists is different than checking for a file. \null must be used after the filename. For example, if you want to see if C:\Windows exists, you would type
    IF EXIST C:\Windows\null ECHO Windows Exists
    \null is only needed for checking if directorys exist.

    Say you want to exit DOS after the batch file is done. At the end of your batch file type EXIT and DOS will close.

    Pausing is used to ask the user if he wishes to continue. To enter a pause command simply type PAUSE in your batch file. It will then say Press any key to contine, press ctrl+c to cancel. This is helpful for asking a user if they are sure.

    Although this is an abrupt ending, it will not be the last. I will work on this batch programming guide some more in the future, but right now I have a lot of stuff to do. Please post any comments good or bad that you have on this guide.
    Red Hot Chili Peppers

  2. #2
    rebmeM roineS enilnOitnA steve.milner's Avatar
    Join Date
    Jul 2003
    Deimos326 - A reasonable basic starting point which (imho) requires some pratical examples to help people get to grips with this subject, so with that in mind:

    Can I add to this tut - One of the most useful batch commands I've found is :

    for %%variable in

    Here is a quick example - DOS does not allow the find command to use wildcards in it's file structure eg find "some text" *.txt doesn't work. (at least the last version of DOS I'm used to)

    Call this file wildfind.bat

    @echo off
    for %%a in (%2) do find %1 %%a
    typing wildfind "some text" *.txt now produces the desired result.

    For the 'newbies to batch' a greater understanding will be gained by understanding what is happening here.

    As a last resort, if you can't find out from other sources PM me and I'll explain. You can then add a section on for %%variable in to your next tutorial.

    IT, e-commerce, Retail, Programme & Project Management, EPoS, Supply Chain and Logistic Services. Yorkshire. http://www.bigi.uk.com

  3. #3
    Join Date
    Dec 2003
    interesting mister Deimos. good job for a n00b

  4. #4
    Join Date
    Dec 2003
    hey i'm co- chief for chiefess.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts