Command Prompt for Windows




Command Grouping

Command grouping allows one or more commands to be specified in place where only a single command is allowed. To group commands together enclose all the commands in brackets (ie. ( and ) ) and separate them with the default multiple command separator character. There are three situations where command grouping is allowed, as follows :-

  1. At the start of a command line. For example :-
    ( ECHO one&ECHO two&ECHO three )
    
  2. Specifying the command part of an IF statement. For example :-
    IF "%VAR%"=="" ( SAY nothing &ECHO found )
    
  3. Specifying the command part of a loop. For example :-
    FOR name IN (*) DO ( SAY Found file &ECHO %name% )
    

When command grouping is used, the complete command can be split across several command lines. The WinOne® Command Prompt will concatenate command lines until the complete command is specified. The WinOne® Command Prompt determines that a command is incomplete, by checking for unbalanced brackets. Command grouping can be used either at the command prompt or with in a batch file. When entering commands at the command prompt and a command contains unbalanced brackets, the More? prompt is displayed, requesting more of the command. For example :-

D:\WINDOWS> IF "%VAR%"=="" (
More? SAY nothing
More? ECHO found
More? )

When a command in a batch file contains unbalanced brackets, the WinOne® Command Prompt will simply continue to concatenate the next command line(s) in the batch file, until the complete command contains balanced brackets.

A command line can contain a maximum of 4095 characters. This includes any command lines that have been concatenated together.

When using environment variables with command grouping, they are instantiated as soon as the complete command is determined. This has a very undesirable effect. Consider the following batch file :-

SET VAR=1
IF %VAR%==1 (
    SET VAR=2
    ECHO %VAR%
)
ECHO %VAR%

Running the above batch file displays 1 on the first line and then displays 2 on the next line. This occurs since the first ECHO %VAR% is instantiated before the SET VAR=2 command is executed. This appears to be the standard behaviour when using brackets for command grouping (eg. CMD.EXE).

The WinOne® Command Prompt extends command grouping to allow for delayed environment variable instantiation, by grouping commands in braces (ie. { and } ) instead of brackets. When braces are used in a command line, then any environment variables that are specified inside the braces are not instantiated until the command inside the braces is about to be executed. Consider the following batch file :-

SET VAR=1
IF %VAR%==1 {
    SET VAR=2
    ECHO %VAR%
}
ECHO %VAR%

Running this batch file produces the desired effect. That is, 2 is displays on both lines.

Note:

Command grouping using brackets and braces can be nested, repeatedly.

It is highly recommended to use braces instead of brackets to group commands together.