Creating Truly Powerful Batch Files

Archived content. No warranty is made as to technical accuracy. Content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

By Brien M. Posey, MCSE

Published in TechRepublic's Windows NT Administrator Report

At first glance, the Windows NT® command prompt appears to be just another MS-DOS window. However, unlike other versions of Windows, Windows NT is a full-fledged operating system that doesn't ride on top of MS-DOS. Because of this, the command prompt is a powerful 32-bit application capable of interacting directly with the operating system. Virtually every Windows NT utility contains command-prompt switches. While such switches may seem archaic in the world of graphical user interfaces, they come in handy if you want to write a batch file to automate a process. In addition to the commands that run programs, Microsoft has included several extra commands in the Windows NT command interpreter that don't exist in other versions of Windows. These commands give you greater control over the operating system from your batch files. In this article, we'll show you how to take full advantage of the command line and of all its features.

The Power of the Question Mark

Because the whole point of this article is to show you how to build truly powerful batch files, we should show you how to figure out which commands to use. When you're writing a batch file, you may begin with an idea of what you want to accomplish but not know the exact command to use. For example, you might know that you need to back up a directory but not know how to do it via a batch file.

Figuring out the necessary command is easier than you might think. To do so, find the program that you want to use in your batch file and right-click on its icon or its listing on the Start menu. Now, choose the Properties command from the resulting shortcut menu. When the item's property sheet opens, go to the Shortcut tab and look at the name of the executable file and its location. For example, if you look at the Backup menu item, you can see that the executable file is ntbackup.exe and that it's stored at %SystemRoot%\system32, as shown in Figure A. (%SystemRoot% is just another way of expressing the location of the directory containing Windows NT.)


Figure A: Find the filename and the location of the utility you wish to automate.

Now that you know the filename and location for the utility you need to use, you can create a batch file command that calls the utility. However, adding a line to a batch file that calls the ntbackup file will merely open the Backup program. To automate tasks, you must be able to control a program from within a batch file. That's where the question mark comes in.

Go to your \%SystemRoot%\system32 directory and type ntbackup /?. A help screen will open, as shown in Figure B, listing the many command-line parameters that you can use to make ntbackup perform various tasks from the command line. This technique is great because it works with the majority of NT programs, including many third-party utilities.


Figure B: You can follow a command with the /? parameter to view the various command-line parameters.

The Extended Command Set

As we mentioned earlier, the Windows NT command prompt lets you use commands that other operating systems don't support. If you'd like to see a list of these commands, you can type cmd /? at the command prompt, as shown in Figure C. Now that you know these commands exist, we'll explain how to use them to build more powerful batch files.


Figure C: Windows NT contains many new and enhanced commands.


In Windows NT, the DEL and ERASE commands are much more powerful than they are in other versions of MS-DOS and Windows. Although the basic syntax remains the same, Windows NT offers several switches that allow DEL to behave like the DELTREE command. Using the appropriate switches, it's also possible to delete files with specified attributes. You can even make the deletion process confirm a delete, or make the deletion process invisible. You can find out all of the details by typing del /? at the command prompt.


Tired of that boring black and gray command prompt? You can use the COLOR command to make your batch files look more like professional programs. To do so, simply use the COLOR command followed by two hexadecimal numbers. For example, the command COLOR 17 makes the screen blue with white writing. You can choose from 16 colors, each of which can be used in the foreground or the background. To see a list of available colors and their corresponding numbers, type color /? at the command prompt.


Windows NT contains enhanced versions of the CD and the CHDIR commands. Although both commands employ the usual syntax, two enhancements are worth mentioning. First, you can add the \D parameter to change drives as well as directories. This parameter could save you a few lines in a batch file. For example, the command:


does the same thing as the following set of commands:


The commands have also been modified to enable extra support for directory names that contain spaces. For example, in the past, if you wanted to change to the \My Documents directory, you had to type cd "\My Documents." With the enhanced commands, it's now possible to enter the command without the quotation marks.


The MD and MKDIR commands have been enhanced to save you a lot of work. Now, you can create large directory structures with a single command. For example, now you can type a command such as:


In the past, you had to create the DATA1, DATA2, and DATA3 directories manually before you could create the DATA4 directory. The command we just typed creates them all at once. For example, the command:


takes the place of the following lines:



You're probably already familiar with the PROMPT command and some of its attributes. For example, the command PROMPT $P$G displays the drive letter and the current path as the prompt. This command has become standard in recent years. However, Windows NT provides additional attributes that let you display things like the current time, the Windows NT version number, or—more importantly—the full network path. You can learn more about the PROMPT command by typing prompt /? at the command prompt.


The PUSHD and POPD commands work together. PUSHD captures the name of the current directory. You can also add the name of a directory that you would like to change to. For example, the command PUSHD \DATA1 will switch you to the DATA1 directory (but remember the name of the directory you're currently in). To automatically return to this directory, simply type POPD.


You're probably already familiar with the SET command, which you use to assign a particular value to an environment variable. For example, the command:


would assign the value C:\TEMP to the variable TEMP. In Windows NT, the SET command has been greatly enhanced to let you perform such actions as combining strings or separating parts of a string. For example, it's now possible to take a string, strip off the first five characters, and copy the next seven characters to another variable. Although space doesn't permit us to discuss all of the intricacies of the SET command in this article, you can find out more about it by typing set /? at the command prompt.


The SETLOCAL and ENDLOCAL commands are used together. When you use the SETLOCAL command within a batch file, any environment changes you make after that point are local to the batch file. For example, if you used the SETLOCAL command followed by the:


command, the batch file would recognize the TEMP variable as containing the string C:\TEMP. However, if you were to run a different batch file or open a different MS-DOS Window, the TEMP variable wouldn't contain this string. To prevent future environment variable changes from being local, you can use the ENDLOCAL command.


The IF command works similarly to the way that it does in MS-DOS or in other versions of Windows. You can still compare error levels and strings and check to see if a filename exists. The NOT parameter also still works. However, where the new and improved IF command really shines is in the added enhancements. For example, when comparing strings, you can now test to see if they're equal, not equal, less than, less than or equal to, greater than, or greater than or equal to. There's even a switch you can use to make the comparison case sensitive or case insensitive. Furthermore, IF now includes a DEFINED command that returns a TRUE value if an environment variable has already been defined. As you can see, the added capabilities of the IF command can greatly enhance your batch files. If you want to know more about the IF command, type if /? at the command prompt.


The FOR command still supports the same parameters that it always has, enabling you to test for and act on the presence of a string in a group of files. However, Windows NT contains several enhancements to this command. For example, you can now work with directories or directory trees instead of just files. You can also work with counters. For example, if you wanted to test for even numbers between 16 and 128, you could use the FOR command to do so. There are even some advanced commands for parsing files and filtering out or changing data. You can read all about these enhancements by typing for /? at the command prompt.


The CALL command allows your current batch file to pass information to and execute another batch file. The CALL command now supports several labels that you can use to pass specific information such as a drive letter, a path name, file names and extensions, and other information. You can read all about the CALL command by typing call /? at the command prompt.


The SHIFT command changes the position of arguments within a batch file. You can even specify the position to which you want to begin shifting the arguments. For example, typing SHIFT /2 begins shifting positions after the second argument. You can shift values in the first (%0) through ninth (%8) positions.


If you've ever written a batch file that had several different sections that could execute depending on the value of a variable, you're probably familiar with the GOTO command. However, in the past, it's always been necessary to create a label at the bottom of the file and add a statement to each section to go to this label. Doing so prevents other sections of the file from running when they aren't supposed to. Figure D shows an example of such a file. Notice how each section of the batch file ends by calling the END section. The END section doesn't actually contain any instructions, but we still had to include it so our batch file would execute properly. However, Windows NT allows you to call a label called EOF without actually creating an EOF section. When the command interpreter reaches such a statement, it ends the batch file.


Figure D: In the past, multisection batch files had to call an END section to terminate gracefully.


The START command launches another application in its own window. The various switches that come with the START command let you start the program maximized or minimized. You can even control the title of the window, the program's priority, memory space, and several other aspects. Furthermore, you can control if you want the batch file to wait for the other program to terminate before continuing to run.

The START command is capable of launching more than just executable files. If a file has a registered file type, the command can open the application owning the file extension and tell the registered application to open the desired file. For example, typing start me.bmp would launch Paint and open the file Me.bmp. You can get the specific parameters of the START command by typing start /? at the command prompt.


The ASSOC command makes it possible to register a file extension from the command prompt. If you simply type assoc at the command prompt, you'll see a list of file extensions and the applications they're associated with, as shown in Figure E. You can assign your own custom extension to a file type using the command:


where .EXT is the three-character extension and FILETYPE is the name of the application associated with the file extension.


Figure E: Typing assoc at the command prompt displays all the current file associations.


When we showed you how to use the ASSOC command, you might have wondered how you could force the application you associated with a file extension to launch when you START a file with that extension. That's where the FTYPE command comes into play. The FTYPE command associates a file type with an application. For example, suppose you had a custom application called MyData that had an executable file named database.exe and saved files with the ZZZ extension. You could associate and FTYPE this program using the following commands:


Now, when you double-click on or START a file with the ZZZ extension, Windows NT will recognize it as a database file, launch database.exe, and open your file.


In this article, we've explored the Windows NT command prompt. I've shown you how to find out what command-prompt options exist in various utilities. I then discussed the various commands that have been added to the Windows NT command interpreter. The power of the question mark is now in your hands.

Brien M. Posey is an MCSE who works as a freelance writer. He also works as a network engineer for the United States Department of Defense. You can contact him via e-mail at (Because of the large volume of e-mail the Brien receives, it's impossible for him to reply to every message; however he reads every message and replies to as many as possible.)