Windows 7 / Getting Started

Grouping Commands with Parentheses

With CMD's command extensions, you can group several command lines inside parentheses, and CMD treats them as one command.

This is useful with the if and for commands. For example, compound statements like the following are possible:

if exist c:\data\myfile.dat (
    echo Myfile.dat exists!
    copy myfile.dat d:\backups
    sort myfile.dat >myfile.out
    print myfile.out
)

Also, grouped commands can be used to collect the output of several programs into one file or pipe with redirection, as in this example:

(dir c:\data & dir c:\temp) >listings.txt

The commands must be separated by new lines or by the &, &&, or || separators I discussed earlier.

You can use grouped commands in batch files or at the command prompt. If you enter grouped commands at the command prompt and haven't yet entered the closing parenthesis when you press Enter, CMD prompts you to continue entering the command line(s) by printing the following:

More?

This isn't really a question, so don't type "yes." Just continue typing command lines and end with a closing parenthesis and whatever goes after it. Alternatively, you can press Enter by itself to stop the prompting.

[Previous] [Contents] [Next]