JSI Tip 0290 - Conditional Processing Symbols, Filters, and Redirection for batch processing.

Jerold Schulman

October 28, 1997

2 Min Read
ITPro Today logo

The Windows NT command language supports Conditional Processing Symbols, Filters, and Redirection. These can be used in batch processing as well as at a command prompt.

Symbols:

The ampersand (&) separates multiple commands on one command line.
The parentheses groups multiple commands.
The semicolon or comma or equal sign (; , =) separate command parameters.
The caret (^) allows you to use a command symbol as text, ignoring the symbols meaning.
The double ampersand (&&) causes the command following this symbol to run if the command preceding the symbol is successful.
The double pipe (||) causes the command following this symbol to run if the command preceding the symbol fails.

Examples:

Dir Directory1&Dir Directory2  -  executes both Dir commands
Dir Directory1=Directory2  -  executes the Dir command on both directories
net use \ServerShare&&echo OK  -  displays ok the first time but not subsequently as the is already used

Redirection characters change where a command gets information from or sends information to:

The greater-than sign (>) sends the output of a command to a file or a device, such as a printer. If the file exists, it is 1st deleted.
The double greater-than sign (>>) sends the output of a command to a file. If the file exists, it is extended, if it doesn't exist, it is created.
The less-than sign (<) takes the input for a command from a file. Examples:

Dir Directory1 > Directory2dirlist.txt  -  creates a new dirlist.txt file with the output of the dir command.
Dir Directory3 >> Directory2dirlist.txt  -  adds the output of this dir command to the file created above.

Filters divide, order, or extract portions of the information that pass through them:

The find command searches files for the string you specify.
The sort command orders files.

Examples:

sort < list.txt > sort.txt  -  orders the lines of list.txt as sort.txt.
find ".EXE" < Dirlist.txt > EXE.txt  -  finds upper case .EXE extensions and creates the lines that contain them in EXE.txt

Here is a batch file I call 1Meg.bat which I use to find all files over 1 megabyte in the target directories:

Usage: 1meg Directory1,Directory2,...Directoryn
output: 1meg_YourUserId.log in the current directory

1meg.bat:

@echo off
dir /o-s /c /n %* > %TMP%%USERNAME%tmp.log
find "replace this string with 14 spaces" %TMP%%USERNAME%tmp.log /V > %TMP%%USERNAME%tmp1.log
find "" %TMP%%USERNAME%tmp1.log /V > %TMP%%USERNAME%tmp.log
find "Volume " %TMP%%USERNAME%tmp.log /V > %TMP%%USERNAME%tmp1.log
find " File(s)" %TMP%%USERNAME%tmp1.log /V > %TMP%%USERNAME%tmp.log
find "%TMP%%USERNAME%TMP" %TMP%%USERNAME%tmp.log /V /I > %0...1meg_%USERNAME%.log
del %TMP%%USERNAME%tmp*.log
exit

NOTE: See tip 5921 How do I tee console messages (stdout and/or stderr)?



Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like