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.
In this tutorial:
- The CMD Command-Line
- CMD Versus COMMAND
- Running CMD
- Opening a Command Prompt Window with Administrator Privileges
- CMD Options
- Disabling Command Extensions
- Command-Line Processing
- Console Program Input and Output
- Using the Console Window
- I/O Redirection and Pipes
- Copy and Paste in Command Prompt Windows
- Command Editing and the History List
- Name Completion
- Enabling Directory Name Completion
- Multiple Commands on One Line
- Grouping Commands with Parentheses
- Arguments, Commas, and Quotes
- Escaping Special Characters
- Configuring the CMD Program
- The Search Path
- Changing the Path
- Predefined and Virtual Environment Variables
- Setting Default Environment Variables
- Built-in Commands
- Extended Commands
- Listing Files with the Dir Command
- Paginating Long Listings
- Printing Directory Listings
- Sorting Listings
- Locating Alternate File Streams
- Setting Variables with the Set Command
- Conditional Processing with the if Command
- Scanning for Files with the for Command
- Using the for Command's Variable
- Processing Directories
- Numerical for Loop
- Getting More Information