The role of setlocal enabledelayedexpansion in Windows batch processing

Source: Internet
Author: User

Set local to deferred extension. In fact, that is: Delay variable, full name delay environment variable expansion, want to advance, variable delay is a must pass! So this part wants you to look seriously.

To better illustrate the problem, let's first introduce an example.
Example 1:

?
1234 @echooff  set a=4  set a=5&echo%a%  pause

Results: 4
Explanation: Why 4 instead of 5? Did you change the value of variable A to 5 before echo? Let's take a look at the mechanism of the batch Run command: The batch read command is read on a row (in addition, for example, for a command, and then all statements closed with a pair of parentheses are treated as a row), and the necessary preprocessing is done before processing, which includes assigning values to variables in that row's commands. We now analyze example 1, batch processing in the run to this "set A=5&echo%a%" before the sentence read the whole sentence and do a preprocessing-the variable A is assigned a value, then%a% of course is 4! (There is no reason why the batch is doing this.) In order to be able to perceive the dynamic change of the environment variables, the batch process designs the variable delay. Simply put, after reading a complete statement, you do not immediately assign a value to the variable in the row, but then assign the value before a single statement executes, that is, "delay" the assignment of the variable. So how do you turn on variable latency? What do you need to pay attention to for variable delay?

Give an example:

Example 2:

?
12345 @echooff  setlocal enabledelayedexpansion  set a=4  set a=5&echo!a!  pause

Results: 5
Explanation: The correct answer was obtained because the variable delay was initiated. The start statement for the variable delay is " setlocal enabledelayedexpansion ", and the variable is enclosed in a pair of exclamation marks !! (note the exclamation mark in English), otherwise there is no effect of variable delay. Analysis of Example 2, first "setlocal enabledelayedexpansion" open variable delay, and then "set a=4" to assign a value of variable A to 4, "set A=5&echo!a!" This is given a value of 5 for variable A and output (because the variable delay is initiated, the batch process can perceive dynamic changes, that is, instead of assigning a value to the row variable first, it assigns a value to the variable during run time, so the value of a is 5).

The role of setlocal enabledelayedexpansion in Windows batch processing

Related Article

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.