Detailed explanation of for loop usage of windows bat script

Time:2020-9-30

The basic form of the for statement of windows bat script is as follows:

In the CMD window: for% I in (command1) do command2 
In batch file: for% I in (command1) do command2

The reason to distinguish between the two environments is that although the behavior of the command statement is basically the same in the two environments, the details are slightly different. The most obvious difference is that in the CMD window, the formal variable I after for must be referenced with a single percent sign, that is% I; in the batch file, the formal variable I is referenced You must use a double hundredth sign,%% I. For convenience, if not emphasized, the following explanation takes the batch file environment as an example.

Let’s take a look at the basic elements of a for statement

  1. For, in and do are the keywords of a for statement;
  2. %%I is a reference to a formal variable in a for statement. Even if the variable l does not participate in the execution of the statement after do, it must appear;
  3. After in, brackets before do cannot be omitted;
  4. Command1 represents string or variable, and command2 represents string, variable or command statement;

Let’s take a look at a demo of a Windows bat script (named Demo1)


@echo off
for %%I in (ABC) do echo %%I
pause

Save as a. Bat file (batch file) and execute. You will see the following information in the pop-up batch window:

The for loop for batch files is so simple. Let’s take a look at the notes for the for statement and run a more complex for loop instance.

  • The formal variable I of a for statement can be changed into any one of 26 letters. These letters are case sensitive, that is,%% I and%% I will not be considered the same variable; the formal variable I can also be replaced with other characters. However, in order not to conflict with the 10 formal variables of% 0 to% 9 in batch processing, please do not replace the% I with any one of% 0 -%% 9;
  • The string or variable represented by command1 between in and do can be one or more. Each string or variable is called an element. Each element is separated by space bar, skip bar, comma, semicolon or equal sign;
  • The for statement extracts each element in command1 in turn, assigns its value to the formal variable I, and takes it to the command 2 after do to participate in the execution of the command; only one element is extracted at a time, and then the command statement after do is executed once, regardless of whether the element is brought to command2 to participate in the operation of command2; when the statement after do is executed once, command1 is extracted The for statement does not declare the end of execution until all elements in command1 have been extracted.

With the above foundation, let’s take a look at the following example. This example modifies some contents in Demo1 (marked as demo2), and the results will be very different:


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

The operation results are as follows:

If the bbs.bathome.cn If the period in this string is replaced by a space, a skip or an equal sign, the execution result will be the same as that of demo2.

Now, let’s analyze the execution of the for statement in demo2 code

1. The for statement uses a comma as a separator to cut the string a, B, C into three elements: A, B and C, which determines that the statement after do will be executed three times;

2. The first execution process is as follows: first, take BBS as the value of formal variable I and carry it into the statement after do for execution, that is, execute echo%% I In this case, the I value is a. therefore, the result of the first execution will display the string a on the screen; the process of the second execution is the same as that of the first execution, except that the value of I has been replaced by the second element in command1, that is, B This is a loop. After the third echo is executed, the entire for statement is executed. At this time, the next statement, the pause command, will be executed.

Advanced usage:

1) Search for files in the current directory?


@echo off
for %%i in (*.*) do echo "%%i"
pause

2) Search all text files in the current directory?


@echo off
for %%i in (*.txt) do echo "%%i"
pause

summary

This article on the windows bat script for loop usage details of this article introduced here, more related windows bat script for loop usage content, please search the previous articles of developeppaer or continue to browse the related articles below, I hope you can support developeppaer more in the future!