(Image: Batch Logo)

Contents

* Batch File
Basics

(this page)

*
Batch File
Examples

*
Advanced
Examples

*
Batch File
Tips

*
Obtain 500+
Batch Files


******



Doctor
DOS Betamax's

DOS
BATCH FILE
TUTORIAL


(Image: DOS Logo)


*
(Image: Diamonds)




(Image: Diamonds)

    This is an elementary introduction to DOS batch files with examples. After the tutorial, you may link to sample files which may be copied or downloaded for your own usage. Each example will be explained as to what it accomplishes and what individual lines do towards that end. An additional tips section will aid you when composing your own batch files, and finally you may obtain 500 & more of the batch files I employ for my own purposes.

    This tutorial will assume that you are running DOS exclusively, although Windows users may also make use of DOS batch files. It is assumed you know the basics of DOS command issuing, its file & directory structures & conventions, and have some understanding of the path statement. Some parts will be geared to users of DOS 5, 6 or newer, but most of these batch files will work on all versions of DOS, including non-Microsoft DOSes.

    There will be some notes to alert you to commands available only in certain versions, but this may not be the situation in all cases. Be sure the batch file is exactly as shown here and it is in a directory in your path statement, or run it from the current directory. Yet if after doing this, when it is run you see: "Bad Command or File Name", or odd things happen, chances are that particular file may not function with your DOS manufacturer or version. If you are unsure of which DOS version you are running, type "VER" at the command prompt.

    Remember, Doctor DOS will not be responsible for any problems encountered through the use or mis-use of anything presented here.

 



 

Here are the Main
Advantages to Using
Batch Files



 
 
  •   Computer operations may be performed with fewer keystrokes.

  •   An extended series of complicated operations may be carried out with one short command.

  •   Shorter commands are easier to remember than a long series of keystrokes.

  •   Less chance of making typing errors.

  •   Major time savings.




 

Batch files make one a power user,
and becoming so allows one to run
circles around point & click
friends and co-workers.




 

BATCH FILE BASICS

EXCEPT FOR THE BATCH FILES THEMSELVES,
INFORMATION BELOW MAY NOT BE REPRODUCED
WITHOUT PERMISSION FROM THE AUTHOR ©


PRELIMINARY: "What is a batch file and why might I need one?"
    A batch file is a series of commands that you might ordinarily issue at the system prompt in order to perform a computer operation. The most common uses are to start programs and to run utilities. Batch files do that with one command instead of the multiple commands usually required. They can be likened to shortcut icons as seen in point-&-click operating systems, but they are much more powerful.

    Using a batch file to start a program often means that your path statement may be made shorter. This means fewer directories through which DOS must search during its operations. Having a shorter path will also leave room for other programs which may require path inclusion in order to function properly.

    Further, sophisticated batch files can improve upon program starting by loading all or part of the program into upper or expanded/extended memory, thus freeing up more lower (conventional) memory. Allowing lots of lower memory means your programs have breathing room and there will be space for utilities to run. Another advantage is that, with one command, a batch file can start the program in the desired configuration *and* request an associated file such as a word processor document or spreadsheet be loaded once the main program is running. This saves the user a search for that document and the issuing of the commands necessary to load it. That one simple command of the user's naming does it all.

    This is similar to Window's "File Association" feature, but with more advantages because a number of batch files could be written to load the same file each with its own, but different, attributes or start-up options. In fact, one could even have the same file loaded into different programs, each time with specific, yet different, options. The user never has to change these configurations manually. Once set up, the batch files do it automatically via the user's selection.

    Using a batch file to run a utility means being able to have direct access to that utility. If there are any often-used specific parameters, they can be included in the batch file and thus save you typing and/or the possibility of mis-typing these parameters.

    DOS batch files can also make decisions to perform operations only if certain conditions exist or don't exist. The most sophisticated ones can even emulate commands not normally available with the DOS operating system.



WHAT YOU'LL BE DOING:
    The first section, below, will help you to understand and then write basic batch files. For those who are afraid of programming, this article will show you how simple it really is. DOS batch files are a good place to start learning programming because they use plain-english commands for the most part. Thus, they are easy to comprehend, even for those of you whose first language is not english.

    You may then move on to other sections which allow you to improve a batch file, and to make it look attractive & easy to decipher when looking back at it from some future point.

    In the Advanced Batch Files article, you'll be shown how to make the file prompt a user for input that allows the file to function in different ways depending on circumstances and desired outcome. You'll also see how to combine small batch files to do larger things.

    You may wish to rename the sample files and perhaps use a different path structure. Go ahead - this tutorial is just for example purposes. You may also see another use for a batch file if it had some modifications. Be creative and try the changes; however, be aware that no responsibility will be taken by Doctor DOS for any problems encountered when using (or mis-using) anything presented here.

    I suggest that you always try out any batch file in a TEST directory. Copy some files there to try out the batch. If it works as you intend, and more importantly, if it does *not* work and you do *not* intend, then transfer it to your regular working batch directory. If anything did go wrong in the TEST directory, it would only affect copies of files and not corrupt or destroy the originals.



GETTING STARTED:
    First realise that a batch file typically takes the following form:

Get Ready to Use the Program or Utility
Run the Program or Utility
Clean Up and/or Restore

    The first part above means to set some initial parameters which prepare the main program or utility to run. They might mean to go to a specific directory, set or change/create an environment parameter, place a message on to the screen, or anything else required for the program/utility to run as you or it requires, as dictated by the task to be done.

    The next part runs the program or utility as directed by you, or in some cases by the batch file itself under guidance by some pre-selected criteria.

    Finally, the last part can be used to display closing messages or a directory listing as confirmation of a utility operation. It might delete temporary files made by the batch file, or it might reset the program or DOS to their defaults. Another use would be to deposit the user in some specific directory after the program or utility has finished.


"What do I do first?"
    To begin, create a directory called "C:\BATCH" where you will place your new batch files. Some people use "C:\BAT", but that bothers me because there would then be a directory with the same name as the batch file extensions, which must use ".bat". For housekeeping and organizational purposes, I try to not use names more than once - especially where conflicts might arise or confusion generated.

    Next, modify your path statement as found in your "AUTOEXEC.BAT" file to include this new directory. I suggest placing it near the start so that DOS will find and execute your batch files quicker by not having to search through unnecessary directories first. I have my "Batch" directory placed third right after the root and DOS directories. So the path statement would read in part: PATH=C:\;C:\DOS;C:\BATCH; and so on. It's also important that the BATCH directory be before any program directories. This is because any program executable with the same name as the batch file will initiate first, bypassing your carefully crafted batch commands. After saving this file, type "C:\AUTOEXEC" or reboot to initiate the change.

    After the AUTOEXEC.BAT completes, select a text editor to use in writing each line of your file. The only thing required is that it be able to save these files in plain DOS (ASCII) text. This can be done by all text editors such as DOS' Edlin and Edit, Windows Note/Wordpad, and a host of independent editors. Word processors like Wordstar, WordPerfect and MS Word also have the capability as long as you save the document in ASCII (plain text). I personally like to use a little text editor called "Tiny Editor" by T. Kihlken.

    Type the files into your text editor as you see them here. DOS is not case sensitive except in certain instances, which will be noted when necessary. Otherwise, one may type all upper, all lower, or some combination. I prefer to use all-caps for this purpose, except for certain instances, or when case sensitivity is an issue. Alternatively, you may screen capture or download this page and edit everything out but the batch file(s) you wish to use.

    When finished, name the file as seen here or use some other name that you'll remember and be able to associate with the batch file and what it does. Be sure to stick to DOS file-naming conventions of up to eight characters before the dot and using a ".BAT" extension so that DOS will know it's a batch file and run it as such. Newer DOS versions allow longer file names but I don't recommend their use. Besides causing extra typing in order to run the file, if you decide to use it yourself on an older system, or pass it on to someone using an older DOS version, the file may not run.

    Recalling the file-name conflicts I touched upon above, you should not name your batch file the same as any other file which is on your computer, if possible. Otherwise, whichever comes first during a DOS path statement search, will execute. This is unless one is in the directory in which the given file resides. In that case, the current directory's file takes precedence. There may be some cases where one wishes to use a batch file with the same name as another file, but to eliminate unexpected results it's generally best to not use files with the same names.

    Finally, remember to place your new file into your "C:\BATCH" directory.


Once you understand the basics, we'll discuss some
improvements to make the file do more things with
the same simple keyboard command. This technique
will be followed throughout these lessons.



YOUR FIRST BATCH FILE:
    Let's select a task that involves only a few steps. I use WordPerfect a lot, so we'll do that one first. I'll show you the complete file and then explain what each line does. The following assumes that you are using WordPerfect 6 and that it resides in a WP60 directory on the `C' Drive. Change these parameters, if they differ from your word processor and directory.

    Note that the indents shown for each example are to make them stand out in your browser. Don't indent the lines in the batch file itself.

        :: WP.bat
        :: Runs WordPerfect
        ::
        @ECHO OFF

        C:
        CD\WP60
        WP

    The first line is the batch file name. This is useful to remind anyone looking at the file which one it is. The second line is a title remark that tells what the file does.

    Note the use of twin colons. DOS will not execute any line with twin colons in front of it, nor display it on the monitor screen. After seeing the colons, DOS ignores anything following and goes to the next line. That is because the colon is an illegal label character. Some people use a single colon, but since DOS uses this to identify a label and the line will be read, I suggest it not be used, except of course, as a label precursor.

    Alternatively, others will use "REM" which is short for "Remark". DOS does not execute these lines either, but it does read them, slowing things down. This is not a problem with today's fast processors and hard drives, but can make a difference on slower computers when the batch file is long and contains a lot of remarks. Regardless, my philosophy is that anything which speeds things should be used.

    I must mention there is a also a problem with "REM" lines which contain redirectors and piping, which I won't get into here because that is beyond the scope of this tutorial. I simply recommend the double colon as the best symbol for placing remarks/comments in a batch file.

    Getting back to the example batch file, for the third line I employ the double colons with the remainder of the line blank. It is used as a separator between the title and the file itself. Although they do not affect the running, or contribute to the mis-running, of the batch file, they should be used here because DOS will display an unnecessary prompt on the screen for any blank line before an "Echo Off" command.

    The fourth line is required to tell DOS not to display the succeeding lines on the screen, and the "@" symbol tells DOS not to show the line itself. The first command in typical batch files usually is "Echo Off". "Echo" means that the keys struck on the keyboard are "echoed" (displayed) on the screen. Since a batch file is just a series of commands, what is typed in the file would be echoed on the screen as DOS executes each of those lines, just as though you were typing each instruction at the command line. However, in most cases, the user is only interested in the end result and does not need to see everything that DOS is doing. Using the "Echo Off" command hides each step the file makes as it works toward completing the requested task. Again, if you wish even *that* line to be hidden, add the "at" sign (@) before it, as in the example above.

    Next is a blank line. DOS ignores blank lines (if they occur beyond an "Echo Off" command), and executes the following line immediately. I insert such lines to separate the various steps the file does to complete its assigned task. It makes no difference to the running of the file but does give each part its own space, making for better readability. This is important for lengthy, complicated files, especially if you go back into them a long time after they were written.

    The succeeding line tells DOS to go to the `C' drive. This is important if you are on a floppy, CD-ROM, RAM, or another hard drive, yet wish to start word processing immediately. The next line changes to the WP60 directory and the final line tells WordPerfect to start.

    There you have it. Save that as "WP.BAT" in your BATCH directory. Now to start the program from the DOS prompt, type "WP" from any drive. If it works, you may now remove "C:\WP60;" from your path statement, making for a more efficient running of DOS. Since the batch file points directly to the correct directory for WordPerfect, DOS does not have to search the path to locate it. Since it no longer has to search, having WordPerfect in the path statement is unnecessary. The fewer directories DOS searches through, the faster it locates what it wants, resulting in a more efficient, and thus faster, operation of your computer.

Note that you should be sure you have told WordPerfect
where its files are via the WordPerfect set-up section.
Removing it from the path may result in some error
messages while running WordPerfect if this has not been
done. Typically, pressing Shift-F1 will display the set-up
screen in WordPerfect.



IMPROVEMENTS: "Can this file do more work for me?"
    I have a screen that is my opening one after boot-up and it's the one to which I always return after exiting any program, save for a few. It's my desktop, which for me, is the `C' drive root directory with a specific custom prompt and a directory listing using an after-market program called Color Directory by Loren Blaney. It gives a wide-format display of the contents of the directory with sub-directories and file types in different colours. You could display the directory contents by incorporating DOS's "DIR" command, if you wish, but Color Directory dresses up the screen and makes it easier to discern file types.

    To return to this example desktop after exiting Wordperfect, the screen must be cleared, the `C' Drive root directory must be returned to and the Color Directory listing displayed. Now, we could add these commands to the end of the above batch files, but since I add it to most batch files I write, it's a better idea to make this its own batch file and then have each initial batch file refer to this new one as part of its list of commands. It will save typing those commands into every batch file and should you decide to change your desktop, only this one specific file need be altered, because all others will refer to this one.


    Let's write the new file first:
        :: CLR.bat
        :: Clears Screen and Returns to the DeskTop
        ::
        @ECHO OFF
        CLS

        C:
        CD\
        C:\BATCH\DR.BAT

    We have already discussed the "@Echo Off" command, the double colons and blank lines, so I will skip them here and for all future discussions. Line 5 is DOS's "Clear Screen" command. It erases everything on the screen except for the prompt and any special screen settings contained within, such as certain coloured text & backgrounds, among other options. Lines 7 & 8 return one to the `C' Drive's root directory.

    The final command is for the Color Directory. It's also a batch file (called "DR.BAT") with one line (after "@ECHO OFF") that directs DOS to the directory containing the Color Directory program's executable file. The full path is given so that DOS will not have to search other directories for the "DR" batch file. I have named this batch file "CLR.BAT", which stands for "Clear Screen, Return to Desktop".

    I can now add this "CLR" command to the end of any batch file to clear the screen and return me to my desktop automatically after completing any task. The reason this works is that after a DOS batch file hands control over to a program, when the program finishes, it returns the reins to DOS which remembers that there is another line in the batch file to run. It therefore runs the "CLR" batch file upon exiting a given program, which then returns me to my desktop.


    Using our word processing example further back,
the file now looks like:
        :: WP.bat
        :: Runs WordPerfect and Returns to the Desktop
        ::
        @ECHO OFF
        
        C:
        CD\WP60
        WP

        C:\BATCH\CLR


FURTHER ENHANCEMENTS: "Could this file do yet *more* work?"
    The preceding improves the original "WP.BAT", but we can enhance it further through WordPerfect's built-in command-line options. I won't get into an explanation of WordPerfect's command-line options here as that would be outside the subject of this article. You'll need to consult the WP manual to learn more, however I'll give you an example of some.

    I like to have WordPerfect use expanded memory (/r), and have it place & read its overflow, buffer, & temporary files on my RAM (F) Drive (/d-) for faster operation and the freeing of lower (conventional) memory. With these options, the batch file then becomes:

        :: WP.bat
        :: Runs WordPerfect and Returns to the Desktop
        ::
        @ECHO OFF

        C:
        CD\WP60
        WP /r /d-F:\TEMP

        C:\BATCH\CLR

    Note to use this, you will have to create a TEMP directory on your RAM drive upon each bootup. Add such a line to your Autoexec.bat. If you don't make use of a RAM drive (and you should!), create the TEMP directory on your C drive and point WordPerfect there. It won't be as fast, but there will be fewer files for WordPerfect to look through when it doesn't place its temporary files in its own, already crowded directory.

    WordPerfect, like many DOS programs, can also load a document upon startup. You might like to use a letter form (which is called a "template" in the word processing world) for most letters you type. Its name might be "LETTER.FRM" and it would likely include your name & address as a header, along with various options you like to use. (Never use the extension "TMP" for a "template" file as that's usually used by DOS and its programs to designate a temporary file). If it's sitting in a subdirectory of WordPerfect's called "Template", the third-last line of the batch file becomes:

        WP /r /d-F:\TEMP C:\WP60\TEMPLATE\LETTER.FRM

    This batch file version might be called "WPLET.BAT".

    Now that you have this basic batch file, a large stable of batch files could be written to run WordPerfect and load any number of different documents. In addition, you may also have your batch file start a WordPerfect macro using the "/m-" switch. A macro is a type of batch file used by many programs. You write it much as you would these DOS batch files except with a ".WPM" extension. (See your word processor's manual for details on macro writing.)

    To run a WordPerfect macro from your DOS batch file, enter the macro option after any other options for WordPerfect. Now, with one simple command, you could start the program from anywhere within DOS, load a letter template, and then have the WP macro load the current project on which you are working. The line might then look like:

        WP /r /d-F:\TEMP C:\WP60\DOCUMENTS\LETTER.FRM /m-PROJECT.WPM

    All this is from one simple batch file that might be named "WPCP.BAT" for "Wordperfect, Current Project". What a huge amount of typing saved! Can you imagine typing those lines every time you wanted to work on that particular project? Plus, with this method, you get the options you want, tailored to each type of program and document upon which you are working, without having to reconfigure a program's defaults.

    Note that these command-line options are done using "slash switches", that is a slash followed by a letter or character string. For more on this aspect of DOS, see DOS Switches, elsewhere at this website.


Tip:

    When you are finished with one current project, rewrite the WordPerfect macro to refer to your next project. This way, the same DOS batch file will always start your current project regardless of what it is. This saves having to remember, or look up, a new batch file name for each new project.




Tip:

    If you always load the current project into the same template (which would be typical), after the first time, save the project and template as one, under the project name. Then redo the batch file to have WordPerfect load the project file directly, rather than having it load a template and then run a macro to load the project into that template. It saves a step and gains a small speed advantage.



In closing, any batch file may be made up
from any system prompt commands.
This allows one to funnel multiple-line
typings down to just one simple command.




¦ Batch File Examples ¦
¦ Advanced Batch Files ¦
¦ Batch File Tips ¦
Return to the
Main DOS Page

Obtain
500+ Batch Files

Go to
Richard Bonner's
Home Page