bat batch processing: FOR command

0 Preface

Batch forcommands mainly have 4 usages:

  • for loop
    for %I in (sequence) do command
  • text processing
    for /f %I in (file) do command
  • Process the result of command execution
    for /f %I in ('command1') do command2
  • path expansion
    for /f %I in (pathset) do command

1 for loop function

1.1 Basic commands

When the batch forcommand is used as a for loop, it is written as follows:
in the cmd window or os.system (shell):

for %I in (sequence) do command

When in the bat file:

for %%I in (sequence) do command
  • for, inand doare the keywords of the for statement, and they are indispensable;
  • sequenceis any sequence that can have only one element. Elements can be variables, strings, separated by space bar, tab key, comma, semicolon or equal sign;
  • sequencemust be ()surrounded by even a single element;
  • %%IIt is a form variable, even if it is not used in the command, it cannot be omitted;
  • The formal variable of the for statement Ican be replaced with any one of 26 letters, and these letters are case-sensitive, that is to say, %%Iand %%iwill not be considered as the same variable;
  • %0In order not to conflict with the ~ %910 formal variables in the batch process , please do not %%Ireplace them with any of %%0~ ;%%9
  • The for statement extracts (sequence)each element in sequence in turn, assigns its value to a formal variable I, and brings it to dothe last command to participate in the execution of the command; after executing the last dostatement once, extract (sequence)the next element in and execute the command again , and so on, until (sequence)all the elements in have been extracted, the for statement declares the end of execution;

1.2 Example: String sequence usage

for %I in (A,B,C) do echo %I

result

A
B
C

2 text processing function

2.1 Basic commands

When the batch for command is used for text processing, it is written as follows:
in the cmd window or os.system (shell):

for /f %I in (file) do command

When in the bat file:

for /f %%I in (file) do command
  • /fIndicates to read the content of the file file, and assign it to 1 in line units %%I
    ) By default, each line is divided into section 1 and section 2 with a space bar as a separator. . . By default, only section 1 is read and assigned to %%I
    2) By default, lines beginning with a semicolon; are ignored

The for /f statement has the following optional parameters:

  • "delims= ": set the delimiter, the default is space
    • Multiple choices are possible, for example, "delims=,; "it means that commas, semicolons, and spaces are used as separators at the same time. Note that there are spaces in the code
    • It can be empty. At this time, "delims="there cannot be any spaces in the double quotes or the next parameter, otherwise it will be understood as using spaces as separators, such as "delims=" or "delims=tokens=1"
  • "tokens=": The number of characters to read in each line, the number of sections starts from 1, and the default is 1
    • Multiple choices are allowed, separated by commas, and used to -represent intervals, such as "tokens=1,3,5", such as"tokens=1-5,6"
    • When "tokens="multiple numbers are specified later, for /fthe statement makes the following provisions for this situation: If the form variable is %%I, then the content indicated by the first number is received by the first form variable %%I, and the content indicated by the second number is received by The second form variable is used to receive, and the content indicated by the third number is received %%Jby the third form variable . %%KAmong them, the formal variables follow the alphabetical order, and the specific symbol of the Nth formal variable is determined by the first formal variable
    • "tokens="can be used to *indicate that the rest of the content as a whole *has a lower priority than the number. For example, "tokens=*"it means that the whole line is regarded as Section 1, "tokens=1,*"and that all content except Section 1 is regarded as Section 2.
  • "skip=n": Indicates to skip the 1st to the nth line and start reading from the n+1 line, the default n=0
  • "eol=": Indicates ignoring lines beginning with the specified character, only one character can be specified, and only the first one will take effect when multiple input is entered. The default is semicolon, can be empty
  • Multiple character parameters are written under a pair of double quotes, such as the following statement is equivalent to the default result
    for /f "delims= tokens=1eol=;"

3 Process the command execution result

3.1 Basic commands

The content in the brackets of the batch for command can be other commands, written as follows:
in the cmd window or os.system (shell):

for /f %I in ('command1') do command2

When in the bat file:

for /f %%I in ('command1') do command2

'command1'Single quotation marks must be added, indicating that the result of command1 execution is treated as a text object for processing

3.2 Example: combining for and dir statements

for /f %%I in ('dir %CD%\*.*  /A-D /B /S /ON') do echo %%I

4 path expansion

4.1 Basic commands

forWhen the command form variables of batch processing %%Iare path information such as path and file name, they can be converted quickly. The writing method is as follows:
in the cmd window or os.system (shell):

for /f %I in (*.*) do echo %~nI

When in the bat file:

for /f %%I in (*.*) do echo %%~nI

The specific instructions for path expansion are:

Expansion of variable %I effect
%~fI expand %I to a fully qualified pathname
%~nI Expand %I to a filename only
%~xI Expand %I to only one file extension
%~nxI expand %I to only a filename and extension
%~dpI Expand %I only to directories, ending with \

4.1 Example: Search all txt files in the current directory and subdirectories, only record the file name and extension

In the cmd window or os.system (shell):

for /f %I in ('DIR %CD%\*.txt  /A-D /B /S /ON') do echo %~nxI>>log.txt

When in the bat file:

for /f %%I in ('DIR %CD%\*.txt  /A-D /B /S /ON') do echo %%~nxI>>log.txt

result:

1.txt
2.txt

5 compatible parameter usebacq

5.1 Path compatibility

In batch commands for /f %I in (file) do command, file paths do not need quotes. However, if there are special symbols such as 空格, &etc. in the file path or file name, it will not be recognized. At this time "usebacq", after using parameters, the file name can be surrounded by double quotes
. Error:

for /f %I in (a&b.txt) do command

correct:

for /f "usebacq" %I in ("a&b.txt") do command

5.2 Command Compatibility

In the batch command for /f %I in ('command1') do command2, if command1 contains single quotation marks, it will not be recognized. At this time "usebacq", after using parameters, command1 can be surrounded by backticks ` (the same key as ~)

correct:

for /f "usebacq" %I in (`command1`) do command2

5.3 String compatibility

In batch commands for %I in (sequence) do command, if the sequence is a string, it needs to be surrounded by double quotes. If the string itself has double quotes as characters, "usebacq"the sequence can be surrounded by single quotes after using parameters
. Correct:

for "usebacq" %I in ('str="a"') do command

—————————————————————————————————
Original link: https://blog.csdn.net/u011079613/article/details/ 119651568

Guess you like

Origin blog.csdn.net/B11050729/article/details/131359772