DEL and ERASE
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.
COLOR
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.
CD and CHDIR
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:
CD /D E:\DATA
does the same thing as the following set of commands:
E:
CD\DATA
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.
MD and MKDIR
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:
MD \DATA1\DATA2\DATA3\DATA4
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:
MD \DATA1\DATA2\DATA3\DATA4
takes the place of the following lines:
CD\MD DATA1
CD DATA1
MD DATA2
CD DATA2
MD DATA3
CD DATA3
MD DATA4
CD\
PROMPT
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.
PUSHD and POPD
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.
SET
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:
SET TEMP=C:\TEMP
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.
SETLOCAL and ENDLOCAL
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:
SET TEMP=C:\TEMP
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.
IF
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.
FOR
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.
CALL
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.
SHIFT
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.
GOTO
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.