Home > Articles > Home & Office Computing > Microsoft Windows Desktop

  • Print
  • + Share This
This chapter is from the book

Creating and Using Batch Files

Batch files are plain-text files. You can most easily create and edit them with Notepad. I suggest that you create a special folder just for your own batch files and add this folder to your search path (as discussed in Chapter 11) so that you can run them from any folder in any Command Prompt window.

You can place them in any folder you want. You can place them on your own hard drive, or you may wish to place your batch files on a shared network folder so they can be used from any computer. I place my personal batch files in a folder named c:\bat. To create this folder, open a Command Prompt window and type these commands at the prompt:

mkdir \bat
cd \bat

To add this folder to the search path, follow these steps:

  1. Click Start, right-click My Computer, and select Properties.
  2. Select the Advanced tab and click Environment Variables.
  3. Look in the list of variables defined under User variables for (your-user-name).
  4. If there is already an entry named PATH, select it and click Edit. At the beginning of the Variable Value field, insert C:\bat and add a semicolon to separate this name from the names that are already there. The result should look something like what's shown in Figure 12.1.

    Figure 2.1

    Figure 12.1 When adding a directory to the beginning of the path, be sure to place a semicolon after the new directory name.

    If there is not already an entry named PATH, click New and enter PATH as the variable name. Enter C:\bat as the variable value.

  5. Click OK three times to close the three dialog boxes. Close the Command Prompt window and open another one. Type path and press Enter. You should see c:\bat at the beginning of the path.

Once c:\bat has been added to your path, any batch files you create in this folder can be run just by typing their names at the command prompt.

Batch files should be given the extension .cmd or .bat. Either is fine. The .bat extension is more traditional, whereas .cmd makes it clear that the batch file is written for Windows NT/2000/XP, because DOS and Windows 9x will not recognize such files as batch files.

To create a sample batch file, open a Command Prompt window or use the one opened earlier in this section. Type the command

notepad test.bat 

and then click Yes when Notepad asks, Do you want to create a new file? In the empty Notepad window, type the following lines:

@echo off 
echo The command line argument is %1

Save the file and at the command line type this:

test xxx 

The Command Prompt window should clear, and you should see the following:

The command line argument is xxx 
Press any key to continue . . .

Press Enter, and the command prompt should return. You can use this procedure to create any number of batch files. One batch file you may want to create right now should be named bat.bat, with this one line inside:

pushd c:\bat 

This falls into the "tiny handy" batch file category mentioned earlier. With this in place, if you type bat at any command prompt, your current directory will be changed to c:\bat so that you can edit or create batch files. Type popd to go back to whatever directory you were using beforehand.

Of course, you could just type pushd c:\bat directly. It may seem silly to create a batch file just to save nine keystrokes, but when you're actively developing batch files, you'll quickly find that this does make life easier.

I have about a dozen batch files like this that I use on a daily basis to move into directories for specific projects. For projects that use special command-line programs, the batch file has a second line that adds the program directory to the beginning of the search path, using a command such as this:

path c:\some\new\folder;%path% 

If you find yourself frequently working with command-line programs, you will want to make "tiny handy" batch files for your projects as well.

  • + Share This
  • 🔖 Save To Your Account