Detailed explanation of the role of setlocal enabledelayedexpansion in Batch Processing

Source: Internet
Author: User

To better illustrate the problem, we first introduce an example.
Example 1: Copy codeThe Code is as follows: @ echo off
Set a = 4
Set a = 5 & echo % a %
Pause

Result: 4
Explanation: Why is it 4 instead of 5? I already changed the value of variable a to 5 before echo? Let's first look at the mechanism of running commands in batch processing: when reading commands in batches, they are read by line (for example, for commands, etc, then, all the statements closed with a pair of parentheses are treated as a row). The necessary preprocessing must be completed before processing. This includes assigning values to the variables in the command line. Now let's analyze Example 1. Before running the batch processing command "set a = 5 & echo % a %, first read the entire sentence and perform preprocessing -- assign a value to variable a, then % a % is 4 of course! (There is no reason why batch processing does this .) To detect the dynamic changes of environment variables, the variable delay is designed for batch processing. To put it simply, after reading a complete statement, the variable in the row is not immediately assigned a value, but will be assigned a value before a single statement is executed, that is to say, "latency" is used to assign values to variables. How to enable variable latency? What do you need to know about variable delay?
For example:
Example 2:Copy codeThe Code is as follows: @ echo off
Setlocalenabledelayedexpansion
Set a = 4
Set a = 5 & echo! A!
Pause

Result: 5
Explanation: The correct answer is obtained due to variable delay. The variable delay start statement is "setlocalenabledelayedexpansion", and the variable must use a pair of exclamation marks "!". . For example 2, "setlocalenabledelayedexpansion" is used to enable variable latency. Then, "set a = 4" assigns variable a value of 4 to variable a, and "set a = 5 & echo! A !" This statement assigns a value to variable a to 5 and outputs the result (because variable delay is enabled, the batch processing can perceive dynamic changes, that is, not assigning a value to the variable in this row first, instead, assign a value to the variable during the running process. Therefore, the value of a is 5 ). Let's take another example to consolidate it.
Example 3:Copy codeThe Code is as follows: @ echo off
Setlocalenabledelayedexpansion
For/l % I in (1, 1, 5) do (set a = % I echo! A! )
Pause

Result: 12345
Explanation: In this example, variable latency is enabled and "!" is used. Expand the variable to get the expected results. What will happen if there is no variable delay? The result is as follows: ECHO is disabled. ECHO is disabled. ECHO is disabled. ECHO is disabled. ECHO is disabled. That is, no dynamic changes in the for statement are detected.
Batman description
Let me give a brief introduction:
Set: set
Local: local (environment variable)
Enable: Yes
Delayed: latency
Expansion: Extended
Setlocal enabledelayedexpansion is to extend the latency of local environment variables,
Compare the following two codes:Copy codeThe Code is as follows: @ echo off
For/l % I in (1, 1, 10) do (
Set "str = % I"
Echo % str %
)
Pause> nul

Copy codeThe Code is as follows: @ echo off & setlocal enabledelayedexpansion
For/l % I in (1, 1, 10) do (
Set "str = % I"
Echo! Str!
)
Pause> nul

The first code shows only 10 lines of "ECHO is disabled .", The second code correctly displays 10 rows of numbers from 1 to 10. Why? Because str is not defined before the for loop of the two segments of code, and the str value is not defined because the variable delay is not enabled for the first segment of code, so 10 rows of reports are displayed.
The variable delay is enabled in the second code. In the for loop, the str value is passed each time, so 10 rows of numbers are correctly displayed, however, the str variable must be written! Str !, There is no reason to say this. Just remember it.

What does setlocal enabledelayedexpansion mean?

Yes: set local to latency extension. In fact, this is the latency variable, which is short for "extension of latency environment variables ",
The script is preprocessed before cmd executes the command. One of the processes is the variable recognition process. In this process, if two variables, such as % value %, are enclosed in %, they will be identified, and the corresponding value of the variable will be searched, and then the value will be replaced with this variable, the process of replacing the value is called variable extension, and then execute the command.
Before explaining this, let's take a look at the differences between several examples:
Example 1:
Set value = kkkkkkk
Echo % value %
Save the code to a text file with the suffix bat. Open dos, go to the corresponding directory, and execute the file. The result is as follows:
C: \ Documents and Settings \ Administrator \ Desktop \ ln \ temp \ bat> set value = kkkkkkk
C: \ Documents and Settings \ Administrator \ Desktop \ ln \ temp \ bat> echo kkkkkkkk
Kkkkkkk
The last line is the result, but there are two sentences before the result: set value = kkkkkkk and echo kkkkkkkkkk. But in the statement, we did not write the echo kkkkkkkkkk statement, this indicates that the value of the variable is replaced at least when echo % value % is executed. This is the extension of the variable.
So what is the variable delay extension?
If you know the concept of "static variables" in C ++, you should know that static variables will be replaced during c ++ compilation, however, this replacement is based on static conditions. This is also true for Variable Expansion. What if there is a dynamic situation? In cmd execution, a dynamic condition occurs when a value is assigned to a variable in the for statement, for example:
Example 2:Copy codeThe Code is as follows: @ echo off
For/l % I in (1, 1, 3) do (
Set k = % I: cyclically assigning values to k
Echo % k % I
)

The following result is displayed when the script is executed:
_ 1
_ 2
_ 3
These three statements appear in the result. _ Space
NOTE: If k does not have an initial value, it is replaced with null.

Example 3:Copy codeThe Code is as follows: @ echo off
Set k = yyy
For/l % I in (1, 1, 3) do (
Set k = % I: cyclically assigning values to k
Echo % k % I
)

Result:
Yyy 1
Yyy 2
Yyy 3
NOTE: If k has an initial value, it is replaced with yyy. ,
Example 4:Copy codeThe Code is as follows: @ echo off
Setlocal enabledelayedexpansion
Set k = 3
For/l % I in (1, 1, 3) do (
Set k = % I
Echo % k % I
)

Result:
3 1
3 2
3 3
The latency variable is already used here. Why does this happen? Let's look at Example 5:
Example 5:Copy codeThe Code is as follows: @ echo off
Setlocal enabledelayedexpansion
Set k = 3
For/l % I in (1, 1, 3) do (
Set k = % I
Echo! K! % I
)

Result:
1 1
2 2
3 3
It was originally used in latency variable extension! To reference variables.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.