Digging deeper from the command line

In the July 1999 Top 10, I shared some useful command-script techniques. In this column, I provide 10 more tips that can add power to your scripts and give them a professional appearance.

10. Use the Cls command to clear the Command Prompt window. Clearing the screen is a good technique to use at the beginning of a script.

cls

9. Use the @title command to give the Command Prompt window a custom title. By default, when a command script runs, it opens a window with the nondescript Command Prompt label, which doesn't provide much information.

@title=<Your Title>

8. Use the @color command to set the color of the Command Prompt window. The @color command accepts a two-digit attribute setting. To see all possible color combinations, enter color/?. The following example sets white on blue:

@color 1F

7. Use the Pushd and Popd commands to save and restore the current directory. To perform actions in different folders, Windows NT command scripts commonly change the current directory. The Pushd command stores the current drive and directory, then changes to the specified drive and directory. Popd changes the current directory back to the directory that the Pushd command stored.

C:\> pushd C:\temp
C:\temp> popd
C:\>

6. Use the Setlocal and Endlocal commands to protect environment variables. Because environment variables are common to all the tasks that run in the system, changing them can alter your system's functionality. Use the Setlocal command to restrict any environment changes to the current script. The Endlocal command restores the previous environment settings. (These commands work only within a script, not when you type them directly on the command line.)

setlocal
endlocal

5. Use the Errorlevel environment to test command results. Many NT commands and other batch-oriented console programs can return information (i.e., error codes) to the calling script that informs you of the program's success or failure. You can use the Errorlevel environment to access this error information (if the program provides it).

net group mygroup myuser /add
if errorlevel 1 echo Net Group Add failed

4. Use quotation marks to work with long filenames. Long filenames make files easier to find. But, if a command accepts more than one parameter, using long filenames can be cumbersome in command scripts.

copy "C:\temp\sales briefing sheet 1.doc"

3. Use redirection for input and output. The following example shows how you would use the dir_in.txt file (which contains the input parameter for the Dir command) to execute the Dir command and send the output to dir_out.txt.

dir < dir_in.txt >dir_out.txt

2.
Use the @findstr command to conditionally take action based on a text string contained in a file. The following example redirects ping output to a file, then checks that file for the phrase timed out and executes the pingerror.cmd script (if the timed out text is present).

ping myserver >ping_output.txt
@findstr "timed out" ping_output.txt && pingerror.cmd

1. Use the For command to parse files (i.e., read a file's contents) and take action based on the contents. The following example shows how to read the contents of a text file containing a list of server names and ping each server:

for /f %%i in (servers.txt) do ping %%I 

End of Article




You must log on before posting a comment.

If you don't have a username & password, please register now.

Reader Comments

Slight error in the syntax of the last command. It should be a lower case i as the last character. I know this is a tiny error but there are plenty of cabbages who might not work it out. Otherwise very useful.

Michael.

Michael O'Neill

Michael Otey's Top 10: "More NT Command-Script Techniques" (December 1999) shows that Windows NT still has characteristics of its MS-DOS ancestor. I have a big MS-DOS 6 book whose sole purpose now is to explain some of the command-line commands I use in NT. I still find this book useful, except for finding out about NT's added functionality for some of the commands. --­Wilbur C. Bragg

Wilbur C. Bragg

Michael Otey's Top 10: "More NT Command-Script Techniques" is very useful. Keep up the good work! I'd like to see more articles about scripting with the NT command interpreter, cmd.exe. --­Carlos Perez

Carlos Perez

I'm an avid reader of Windows 2000 Magazine. I always read Michael Otey's Top 10 column, which usually brings up items I've forgotten that are so easy to use. For example, Top 10: "More NT Command-Script Techniques" (December 1999) discusses simple--­and useful--­ Windows NT command-script techniques. I was trying to write a script that would read a text file. I tried learning KiXtart, Windows Script Host (WSH), and Visual Basic (VB). I wasted a lot of time trying to learn these programming languages when all I needed to do was apply the article's number 1 tip: Use the /f switch in a For command. I felt as if I had sailed the open seas for years and finally found land! This tip has come in handy for so many of my scripts. I want to know where to find a reference that shows other switches like this one. I have many books about DOS, but none of them mention using the /f switch with the For command. --­Loreto Manni

Loreto Manni

<i>Apart from my column and Windows 2000 Magazine's Win32 Scripting Journal newsletter (http://www.win32scripting .com), another good resource for NT command-script techniques is Tim Hill's book Windows NT Shell Scripting (Macmillan Technical Publishing). --­Mike Otey</i>

Mike Otey

Just a little point concerning tip 4. For directories with spaces in them, use the first six characters folowed by "~1".

Example: Directory name = My Documents type in command prompt = mydocu~1

Hamish Robertson

 
 

ADS BY GOOGLE