FOR Command Ignores Pipe (81089)



The information in this article applies to:
    Microsoft MS-DOS operating system 5.0
    Microsoft MS-DOS operating system 5.0a
    Microsoft MS-DOS operating system 6.0
    Microsoft MS-DOS operating system 6.2
    Microsoft MS-DOS operating system 6.21
    Microsoft MS-DOS operating system 6.22

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):

"The MS-DOS Encyclopedia," Microsoft Press, 1988

"MS-DOS Batch Files," Second Edition, Kris Jamsa, Microsoft Press, 1991

Modification Type: Major Last Reviewed: 5/12/2003
Keywords: KB81089