On Computers

Batch Files 101  

Gail Allinson, gail@oncomputers.info

4 May 2003

Batch files are text files which contain a sequence of commands.  If what I just said was totally incomprehensible, that's okay.  You are here to learn. Batch files are generally used for tasks that would otherwise require typing several separate commands at the DOS or Command Prompt.  They are often used for tasks that will be repeated.  One of the most common tasks people write their own batch files for is deleting temporary files.

So let's break it down. What is a command?  In this case, we are talking about MS-DOS commands and batch commands.  Just about any MS-DOS command that will work in your version of Windows will work in a batch file.  A batch command is just a special command that is used in a batch file.  You can learn about basic DOS commands using the results of this Google Search -- Google Search MS-DOS. Remember that each version of DOS is somewhat different and has different commands available. Sometimes commands work differently in different versions of Windows. This is very true of Windows XP vs. earlier versions of DOS (see the article "New ways to do familiar tasks" in the Windows XP Help and Support Center).   You can also get more information at the Command Prompt in Windows XP by typing command /? or help command where "command" is the actual name of the command you are using, dir /? or help dir for example.

If you are not familiar with MS-DOS and especially if you are using Windows XP where Microsoft has taken great pains to hide DOS from you, you are probably going to want to use the command line interface, also called the "Command Prompt".  You can find it on the Start Menu (under All Programs) in Accessories, or simply chose Run from the Start menu and type cmd.exe.  When you do that, you should see a window with a black background (if no one has changed the color) and whitish or grayish lettering.  There will be a little flashing underline. Type dir and hit the Enter key on your keyboard.  You have just executed the command to list the contents of a folder which is also called a directory.  It is not as pretty as Windows Explorer, but it will do the job.  "Dir" is just one of may DOS commands.

This is what happens when you type dir at the Windows XP command prompt
This is what happens when you type dir at the Windows XP Command Prompt.

Now that you know something about what a command is, let's talk a little about the file itself.  A batch file can be created using any text editor. Since every version of Windows has a basic text editor called Notepad, it is convenient to use Notepad for creating and editing batch files.  In order to do create the batch file you simply type your sequence of commands on separate lines in Notepad and save the file as a text file.  Then you rename it using a .bat extension.  For example, you write a file and save it as myfile.txt.  You would rename it as myfile.bat to make it a batch file.  Please note that Windows will warn you not to do that, but don't worry about the warning; it is only to help prevent you from accidentally giving a file the wrong extension. When Windows "sees" the .bat extension it "knows" that the file should be run using the command interpreter.  It is less common, but you can also use the .cmd extension for a batch file and Windows will handle it appropriately.  For general purposes stick with .bat.

By searching for *.bat I found over 100 batch files on my computer
By searching for *.bat I found over 100 batch files on my computer.

A good way to learn is to look at the existing batch files on your computer.  Search your computer for *.bat files to see what batch files you already have.  I was surprised to find that I have over 100 batch files.  You may have a different result because most of the batch files on my computer were put there by the various programs that I have installed. Be careful that you do not double-click on a batch file without knowing what it does.  Batch files can be very powerful and can be used to delete entire folders from your hard drive.  In order to look at a batch file, right click on it and choose edit from the context menu.  This will open the file in Notepad and allow you to look at it. If desired you can edit it, too.

This is as good a point as any to mention that autoexec.bat is a special batch file that is read when you boot a DOS disk.  It is present in all versions of Windows including Windows XP, but in Windows XP it is empty.  Back in the "good old days" it was usually the first batch file you saw and worked on.  In fact, it was difficult to run MS-DOS or early versions of Windows without editing autoexec.bat from time to time. In Windows XP it is much better to leave autoexec.bat alone.

I've told you what batch files are and how to look at them on your computer, but how do you run them? You run them either by double clicking on the icon or on a shortcut (single-click if your have Windows set up that way) just like any other Windows program.  You can also type their name, with or without the extension (you need the path if they are in a different folder) using Run on the Start Menu or type the batch file's name from the Command Prompt. 

You can also make shortcuts to batch files and double-click on the shortcut.  I like to make a shortcut by right-clicking on the file, dragging my cursor to where I want the shortcut, and choosing "Create Shortcut Here" from the context menu.  Once you have made a shortcut, you can change the icon to anything you want by right clicking and choosing "Properties".  On the shortcut tab, select the change icon button.  You may get a warning about your file containing no icons, but click "OK" and icons that you can choose from will appear. 

You can change the icon on the shortcut to a batch file using the "Change Icon" dialog.
You can change the icon on the shortcut to a batch file using the "Change Icon" dialog.

One thing I haven't done is to give you a tutorial on how to actually create a batch file that will do something useful.  There is an excellent tutorial at Computer Hope for those who want to learn to write their own batch files.  It also includes some batch commands and some examples.  There is some other useful information at Microsoft TechNet.  And for a few of my favorite ready-made batch files see Fred Langa's Batch Cleanup (be careful with these -- they delete stuff).


Jack's 2 Cents Worth About Batch Files

Jack 'daWabbit' Imsdahl jack@oncomputers.info

For the longest time (years) I used special applications to launch other applications in groups. It made sense to me because I work with applications in groups, more often than not, and I wanted to save clicking on all those icons to set up my workplace. There were (and probably still are) a lot of such 'application loaders' out there as freeware or shareware, so I had no trouble finding them. 

Someone, probably either Buddwig or Al K; pointed out that I was wasting resources running a 'proper' application to do what a simple batch file could do. Being of the 'old school' and recalling only too well when computer resources were much scarcer and more expensive than they are today (even for me) and how things slowed down if one wasted resources, I paid heed and started learning about batch files. 

Here is the first batch file I ever wrote (which my still having proves I am as bad as anyone else about removing the outdated stuff from my system);

start mspaint.exe
start notepad.exe
start wordpad.exe
start calc.exe

This file starts MS Paint, Notepad, Wordpad and the calculator included with Windows in that order. There's nothing special about it, as you can easily see. It was written for Windows 98 and still runs fine on Windows 2000 Pro and XP Home. However, please note that on 2K Pro and XP Home, the first two lines and the last line don't seem to be needed. It runs just fine without them, like the next example. (Please note that the 'exit' command is sometimes needed to close the DOS or Command Prompt window after the file executes.)

start mspaint.exe
start notepad.exe
start wordpad.exe
start calc.exe

If you wish to try these, simply copy and paste them into Notepad or any text editor and save them with the '.bat' extension, then double click on the saved file.

Those two foregoing files illustrate a saving grace given to those of us who are learning batch file creation. Not all commands need to be in all batch files and syntax is much more flexible than one might think. Of course, this also makes it possible to create a batch file that will run on one machine and not another, so there is a downside. This shouldn't worry you too much. You can play with it a bit and, if you are careful, do no harm at all. The worst thing that will happen is that your new creation might not run or might not run in the manner you wish it to. 

A quick search of download.com or similar sites will turn up a lot of batch file creation applications. I have only tried a few but generally found it easier to 'roll my own' in a text editor. One need only type a very few characters, after all, and they're easy enough to figure out, so starting a special application to generate these simple files never seemed worth the while to me.

I have found a couple applications which resist being started from a batch file until one gets the syntax just so. A little patience in trying other ways to set up the batch file fixed this. There are a couple exceptions on our machines. A couple applications (all written in Borland Delphi 4 or 5, which may or may not be relevant) refuse to start from batch files no matter how I try. But out of the 120 or so applications I have installed on this machine, almost all will start from a batch file just fine. The reluctant ones may well start fine were I more experienced in batch file creation.

As Gail said in her very nice article; you can do harm with a batch file if you're telling Windows to delete or move files. But to just start applications, as the ones above do, you need have no fear. And some small amount of study and thought will show you many more uses for batch files. They are a much, much more powerful tool than these illustrations show and their uses are manifold.


© 2003 Gail Allinson and Jack Imsdahl

Back • Home • Up • Next

© 2002 - 2004 by On Computers and the Videotex Services Coalition.