FOR (command line command)

from Wikipedia, the free encyclopedia

FOR is a command line command that enables a loop similar to that in many programming languages ​​( For loop ). While FOR was still a primitive tool in MS-DOS that could only iterate through files within a folder, the FOR command with the command line extensions introduced in Windows 2000 in cmd.exe was expanded into a powerful command that also - with certain Limitations - Can parse text files and iterate through the constituent parts.

Functionality of FOR under MS-DOS

The syntax of the FOR command is fundamentally different from the syntax of other commands on the command line. It reads as follows:

FOR %I IN (*.HTM) DO TYPE %I

The components of a syntactically correct FOR command are:

  • %Irefers to the variable that is replaced by the respective file name during the runtime of the For loop. Any letter of the alphabet can be used as the variable name. If a for loop is to be used within a batch processing file , the percent sign must be doubled: %%Iinstead of %I.
  • INis a keyword that introduces the specification of the files to be iterated. It must appear after the variable name.
  • (*.HTM)refers to the files that are iterated through, in this example all files with the file extension HTM. Both specific files and wildcards can be specified here; It is also possible to specify several types of files separated by a space, e.g. B. (*.HTM *.TXT)for all files with the file extension HTM or TXT. The brackets are mandatory. It is also possible to enter a character string here (in quotation marks), but this is rarely useful.
  • DOis another keyword that introduces the command to be executed. It must necessarily follow the specification of the files.
  • TYPE %Idenotes the command that is executed for each file individually; the variable is replaced by the specific file name during runtime.

Functionality of FOR from Windows 2000

From Windows 2000 onwards, FOR with activated command line extensions offers further, much more powerful options.

Iterating through a file or issuing a command

If FOR is called with the parameter /F, the command line interpreter is instructed to run through the contents of a file and to split each individual line of this file into individual components; therefore it is a primitive tokenizer . It is also possible to evaluate the output of another command in the command line immediately or to specify a character string that is to be evaluated directly in the command.

By default, the content of the file is divided into tokens, whereby the space character and the horizontal tab character are preset as separators and only the first token of each line is evaluated. If these default settings are to be changed, they must be specified using special keywords within quotation marks after the parameter and before specifying the variable:

tokens=defines which token is evaluated in a line. The default value is 1. It is possible to specify a single number, multiple numbers or a whole range of numbers, such as B. tokens=2,4-6to evaluate the 2nd, 4th, 5th and 6th token in each line. If more than one token is specified, the command line interpreter automatically assigns variables to the other tokens, following the alphabet. If% I were specified as the variable name in the command, the other tokens would automatically receive the variables% J,% K and% L. It is also possible to enter the asterisk as a wildcard, for example tokens=3*to evaluate the 3rd and all subsequent tokens.

delims=Specifies the separator that the command line interpreter uses to separate the lines into individual tokens. Multiple separators can be specified, e.g. B. delims=.:to specify the period and the colon as separators.

skip=specifies how many lines at the beginning of the file should be skipped, for example because they contain metadata that is worthless for analysis. The default is 0.

eol=defines the character from which the command line interpreter should stop reading the line. The standard is the semicolon; This has the consequence that lines beginning with a semicolon are ignored by the command line interpreter, similar to comment lines.

usebackqchanges the syntax of the FOR command. By default, the command line interpreter interprets the information within the brackets after the keyword IN as follows:

  • Without special characters, e.g. B (index.txt).: Name of the file whose content is to be processed
  • With single quotes, e.g. B ('dir /b *.txt').: Command whose output is to be processed
  • With double quotes, e.g. B ("1.txt 2.txt 3.txt").: Character string to be processed literally

If the keyword usebackqis now specified, the syntax changes as follows:

  • With grave accents , e.g. B (`dir /b *.txt`).: Command whose output is to be processed
  • With single quotes, e.g. B ('1.txt 2.txt 3.txt').: Character string to be processed literally
  • With double quotes, e.g. B ("Neue Datei.txt").: Name of the file whose content is to be processed

This has the advantage that file names can also be specified with spaces; without the keyword, the components before and after the space would be processed separately (i.e. as a file Neueand as a file Datei.txt).

Recursive iteration through a directory tree

The parameter /Rmakes it possible to search an entire directory tree recursively and to execute the command in the For loop for each file in the individual subfolders that correspond to the specification in the brackets.

The directory tree that is to be searched recursively is specified after the parameter and before the variable is specified. If no directory tree is specified, the current directory is used.

Iteration through folders

If the parameter is specified, /Dthe for loop iterates not through files but through folders.

There is the undocumented possibility of combining the parameters /Dand /Rto search a directory tree recursively for folders.

Iteration through number ranges

If FOR is called with the parameter /L, the For loop iterates through a specified range of numbers.

The syntax for this is (1,1,5). The first number indicates from where the counting should start, the second number by how much the variable should be increased with each iteration of the loop and the third number, from where the counting should end.

More functions

With command line extensions enabled, the FOR command can also be nested. In this case, the command to be executed can be put in brackets to make it clear to which level the command belongs. Breaking out of an inner loop (similar to the key word breakin programming languages ​​such as C) is only possible via a detour by shifting the inner for loop into its own routine; this can then be exit /bended with.

Web links