Skip navigation

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

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 <drive:>\Directory1&Dir <drive:>\Directory2  -  executes both Dir commands
Dir <drive:>\Directory1=<drive:>\Directory2  -  executes the Dir command on both directories
net use <drive:> \\Server\Share&&echo OK  -  displays ok the first time but not subsequently as the <drive:> 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 <drive:>\Directory1 > <drive:>\Directory2\dirlist.txt  -  creates a new dirlist.txt file with the output of the dir command.
Dir <drive:>\Directory3 >> <drive:>\Directory2\dirlist.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 <drive:>\Directory1,<drive:>\Directory2,...<drive:>\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 "<DIR>" %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)?



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish