Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
en:docs:cmd:other:grouping [2014/05/24 06:45] – created valerius | en:docs:cmd:other:grouping [Unknown date] (current) – removed - external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ===== Command Grouping ===== | ||
- | |||
- | Command grouping allows you to logically group a set of commands together by enclosing them in parentheses. The parentheses are similar in function to the [[BEGIN]] and [[END]] block statements in some programming languages. | ||
- | |||
- | 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 want to execute two different [[REN]] commands in all subdirectories of your hard disk. You could do it like this: | ||
- | |||
- | <code bat> | ||
- | [c:\] global ren *.wx1 *.wxo | ||
- | [c:\] global ren *.tx1 *.txo | ||
- | </ | ||
- | |||
- | But with command grouping you can do the same thing in one command (enter this on one line): | ||
- | |||
- | <code bat> | ||
- | [c:\]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. | ||
- | |||
- | 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. For example, the first two of these sequences will work properly, but the third will not: | ||
- | |||
- | <code bat> | ||
- | for %f in (1 2 3) (echo hello %f & echo goodbye %f) | ||
- | for %f in (1 2 3) ( | ||
- | echo hello %f | ||
- | echo goodbye %f | ||
- | ) | ||
- | for %f in (1 2 3) (echo hello %f | ||
- | echo goodbye %f) | ||
- | </ | ||
- | |||
- | 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 uses the ECHO command to create a file (with >), and to append to the file (with >> | ||
- | |||
- | <code bat> | ||
- | 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: | ||
- | |||
- | <code bat> | ||
- | (echo Data files %_date & dir *.dat & echo. & echo Text files | ||
- | %_date & dir *.txt) > filelist | ||
- | </ | ||
- | |||
- | The redirection, | ||
- | |||
- | 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 " | ||
- | |||
- | <code bat> | ||
- | [c:\] (echo Data files %_date | ||
- | More? dir *.dat | ||
- | More? echo. | ||
- | More? echo Text files %_date | ||
- | More? dir *.txt) > filelist | ||
- | [c:\] | ||
- | </ | ||
- | |||
- | A group of commands in parentheses is like a long command line. The total length of the group may not exceed 2,047 characters, whether the commands are entered from the prompt, an alias, or a batch file. The limit **includes** the space required to expand aliases and environment variables used within the group. In addition, each line you type at the normal prompt or the **More?** prompt, and each individual command within the line, must meet the usual length limit of 1,023 characters. | ||