Microsoft KB Archive/81089

= FOR Command Ignores Pipe =

Article ID: 81089

Article Last Modified on 5/12/2003

-

APPLIES TO


 * Microsoft MS-DOS 5.0 Standard Edition
 * Microsoft MS-DOS 5.0a
 * Microsoft MS-DOS 6.0 Standard Edition
 * Microsoft MS-DOS 6.2 Standard Edition
 * Microsoft MS-DOS 6.21 Standard Edition
 * Microsoft MS-DOS 6.22 Standard Edition

-



This article was previously published under Q81089



SUMMARY
The MS-DOS filter, | (pipe character), cannot be used directly in a FOR loop. The pipe command must be used indirectly, perhaps by calling it from a batch file.



MORE INFORMATION
The following MS-DOS command appears as if it will display all .TXT files in a directory using the MORE filter command for easy viewing. This command will display all of the files, but it does not properly execute the pipe (|) or the MORE filter.

for %x in (*.txt) do type %x | more

In the example above, the pipe command will not be executed because the pipe is associated with the FOR command itself, and not the command that FOR is executing (TYPE). That is, the hierachical parsing order of COMMAND.COM interprets this command as

(for %x in (*.txt) do type %x) | more

rather than

for %x in (*.txt) do (type %x | more)

This behavior cannot be changed. If you want to use a pipe in a FOR loop, you must use an intermediate batch file. For example:

MS-DOS Command Line
for %x in (*.txt) do two.bat %x

Contents of TWO.BAT
@type %1 | more

If you want, you can place the FOR command in a batch file that also uses TWO.BAT, as follows:

Contents of Batch File
for %%x in (*.txt) do call two.bat %%x

Note that using a FOR loop in a batch file requires that you use two percent (%) characters to refer to the variable.

Reference(s):

&quot;The MS-DOS Encyclopedia,&quot; Microsoft Press, 1988

&quot;MS-DOS Batch Files,&quot; Second Edition, Kris Jamsa, Microsoft Press, 1991

Additional query words: 6.22 2.1 3.0 3.3 4.01 5.00 5.00a 6.00 6.20

Keywords: KB81089

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.