June 29th, 2006, 12:58 AM
How to Create bat files....
Orignally Taken from
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
This tutorial will assume that you are running DOS exclusively, although Linux, Macintosh, OS/2, Unix and Windows users may also make some, if not complete, usage of DOS batch files. However, be aware, users of those systems will likely need to modify some of the examples in order to comply.
It is assumed you know the basics of DOS command issuing, its file & directory structures & conventions, and have some understanding of pahts and the path statement. Some parts will be geared to users of newer DOS versions, but most of these batch files will work on all versions and manufacturers of stand-alone 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 you run it and get: "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. Again. modifications may be required to make the examples work on your system.
Remember, Doctor DOS will not be responsible for any problems encountered through the use or mis-use of anything presented here.
Batch files make a person a power user, and becoming so allows
one to run circles around point & click friends and co-workers.
Here are the Main Advantages to Using Batch Files
Fewer keystrokes required to perform Computer operations.
Less chance of making typing errors.
Short commands are easier to remember than a long series of keystrokes.
A single command executes an extended series of complicated operations.
Major time savings.
Decisions can be automatically made for the user.
Batch File Tutorial
"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 batch files 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. This same capability may be had at the command line, but the commands will likely be intricate and difficult to remember. Why not let a batch file do the work for you?
With one command, a batch file can start the program in the desired configuration and in addition, can 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. 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. As just mentioned, once set up, the batch files can load the program in the user's chosen configuration automatically with the user's choice of command-line options.
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 options, they can be included in the batch file and thus save you from typing, and 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. You'll also see how to combine small batch files to do larger things.
On the Advanced Batch Files page, you'll be shown more complicated example batch files which use command-line parameters. These allow the file to function in different ways depending on the circumstances and the desired outcome. On the other advanced pages, there are batch files given that prompt a user for input. These permit operations based on choices made by the user after the file is running.
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 as 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.
First realise that a batch file typically takes the following form:
* Get Ready to Use the Program or Utility
* Run the Program or Utility
* Verify and/or Clean Up and/or Restore
The first line above, means to give some initial instructions that prepare the main program or utility to run. These might be to go to a specific directory, create or change 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 line runs the program or utility as directed by you, or in some cases, by the batch file itself under the guidance of some pre-selected criteria.
Finally, the last line 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 restore the program or DOS to its default settings. Another use would be to deposit the user in some specific directory after the program or utility has finished, or return one to where one was before running the batch file.
"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 second right after the C:\DOS directory. So the path statement would read in part: PATH=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" and press "ENTER", 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 Tom 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. For programs, however, I do tend to use the same name as the given program's executable unless it is a long name. This is not generally a problem because I have no program directories in my path and never manually run programs from their own directories.
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. Ordinarily, without a batch file, one would log onto the appropriate drive, change to the WordPerfect directory, and then issue the command to start WordPerfect. A batch file will do this for you automatically. As you will see, each main line in the batch file emulates what one would ordinarily type when at the command line.
I'll first show you the complete batch file and then explain what each line does. The following assumes that you are using WordPerfect 6.0 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. You don't need to indent the lines in the batch file itself unless you want to use them as part of your batch file layout. Indents made by tabs or spaces are ignored by DOS during batch file execution.
:: Runs WordPerfect
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 batch file line with twin colons in front of it, nor display it on the monitor screen. After seeing the second colon, DOS ignores anything following and goes to the next line. That is because the colon is an illegal label character. With regards to batch files, a DOS "label" is a word or a series of numbers/characters used to identify a part of a batch file. Some people use a single colon to place remarks in a batch file, 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. (You'll see more on labels in the tutorial's examples.)
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 into 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 means each command issued by the file as it works toward completing the requested task will not appear on the screen. 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 changes to the correct directory for WordPerfect, DOS does not have to search the path to locate the WP executable file. Since it no longer has to search, having WordPerfect in the path statement is unnecessary. The fewer directories through which DOS must search, 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 program's set-up section.
Removing the WordPerfect directory 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 needs to be altered, because all others will refer to this one.
Let's write the new file first:
:: Clears Screen and Returns to the DeskTop
We have already discussed the opening information lines, the "@Echo Off" command, the double colons and blank lines, so I will skip them here and for all future discussions. Lines 6 & 7 return one to the `C' Drive's root directory. Line 8 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.
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 any 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 farther back,
the file now looks like:
:: Runs WordPerfect and Returns to the Desktop
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 those 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:
:: Runs WordPerfect and Returns to the Desktop
WP /r /d-F:\TEMP
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 may be 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 a ".WPM" extension is used in Wordperfect. (See your word processor'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 into that template. 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. Making use of command line options (switches) is a very powerful way to run programs.
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.
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, a 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 files put one on the way to power-user status!
_____________________________________________________________________Edit:forgot to Add Author's Site
Edit Completed the half Part