Command grouping allows you to group a set of commands together logically by enclosing them in parentheses.

 

There are two primary uses for command grouping. One is to execute multiple commands in a place where normally only a single command is allowed. For example, suppose you wanted to execute two different REN commands in all subdirectories of your hard disk. You could do it like this:

 

global ren *.wx1 *.wxo

global ren *.tx1 *.txo

 

But with command grouping you can do the same thing in one command:

 

global (ren *.wx1 *.wxo & ren *.tx1 *.txo)

 

The two REN commands enclosed in the parentheses appear to GLOBAL as if they were a single command, so both commands are executed for every directory, but the directories are only scanned once, not twice, typically saving time.

 

This kind of command grouping is most useful with the EXCEPT, FOR, GLOBAL, and IF commands. When you use this approach in a batch file, you must either place all of the commands in the group on one line, or place the opening parenthesis at the end of a line and place the commands on subsequent lines. Examples 1 and 2 below will work properly, but Example 3 will not:

 

Example 1 (correct):

 

for %f in (1 2 3) (echo hello %f & echo goodbye %f)

 

Example 2 (correct):

 

for %f in (1 2 3) (

 echo hello %f

 echo goodbye %f

)

 

Example 3 (incorrect):

 

for %f in (1 2 3) (echo hello %f

 echo goodbye %f)

 

If the above examples are typed at the command line, TCC will issue a More? prompt in response to each line until the command group is closed (i.e. the final parenthesis is recognized) as discussed below.

 

The second common use of command grouping is to redirect input or output for several commands without repeatedly using the redirection symbols. For example, consider the following batch file fragment which places some header lines (including today's date) and directory displays in an output file using redirection. The first ECHO command creates the file using >, and the other commands append to the file using >>:

 

echo Data files %_date > filelist

dir *.dat >> filelist

echo. >> filelist

echo Text files %_date >> filelist

dir *.txt >> filelist

 

Using command grouping, these commands can be written much more simply. Enter this example on one line:

 

(echo Data files %_date & dir *.dat & echo `` & echo Text files %_date & dir *.txt) > filelist

 

The redirection, which appears outside the parentheses, applies to all the commands within the parentheses. Because the redirection is performed only once, the commands will run slightly faster than if each command was entered separately. The same approach can be used for input redirection and piping.

 

You can also use command grouping in a batch file or at the prompt to split commands over several lines. This last example is like the redirection example above, but is entered at the prompt. Note the More? prompt after each incomplete line. None of the commands are executed until the command group is completed with the closing parenthesis. This example does not have to be entered on one line:

 

[c:\] (echo Data files %_date

More? dir *.dat

More? echo.

More? echo Text files %_date

More? dir *.txt) > filelist

[c:\]

 

Limitations

 

A group of commands in parentheses is like a long command line. The total length of the group is only limited by your available RAM.

 

You cannot use TEXT / ENDTEXT, or GOTO or GOSUB labels in a command group.

 

Each line you type at the normal prompt or the More? prompt, and each individual command within the line, must be within the usual command line length limit.