In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about the usefulness of setlocal enabledelayedexpansion in batch processing. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Set locally to delay extension. In fact, that is: delay variable, full name delay environment variable expansion, if you want to advance, variable delay is a must! So I hope you can take a closer look at this part.
In order to better illustrate the problem, let's introduce an example first.
Example 1:
@ echo off set axi4 set a=5&echo a% pause
Results: 4
Explanation: why 4 instead of 5? Have you changed the value of variable a to 5 before echo? Let's first take a look at the mechanism by which the batch runs the command: the batch reads the command by line (for example, the for command, etc., and all statements closed with a pair of parentheses are treated as one line), and the necessary preprocessing should be completed before processing, including the assignment of variables in the command of this line. Let's now analyze example 1. Before the batch process runs to the sentence "set a=5&echo% a%", the whole sentence is read and preprocessed-- if the variable an is assigned a value, then% a% is of course 4! (for no reason, that's what batch processing does. In order to be able to perceive the dynamic changes of environment variables, batch processing designs variable delays. To put it simply, after reading a complete statement, the variable of the row is not assigned immediately, but before a single statement is executed, that is, the assignment of the variable is "delayed". So how do you turn on variable delay? What do you need to pay attention to about variable delay?
Give an example to illustrate:
Example 2:
@ echo off setlocal enabledelayedexpansion set axiom 4 set a=5&echo! a! Pause
Results: 5
Explanation: because the variable delay was started, the correct answer was obtained. The delayed startup statement for the variable is "setlocal enabledelayedexpansion", and the variable uses a pair of exclamation marks "!!" Wrap it up (pay attention to the English exclamation mark), otherwise there will be no effect of variable delay. Analyze example 2. First, "setlocal enabledelayedexpansion" turns on variable delay, and then "set axiom 4" first assigns variable a to 4, "set a=5&echo! a!" This sentence assigns the variable a to 5 and outputs (because of the delay in starting the variable, the batch can sense the dynamic change, that is, it does not assign a value to the variable first, but to the variable during operation, so the value of an is 5). Give me another example to consolidate it.
Example 3:
@ echo off setlocal enabledelayedexpansion for / l%% I in (1jie 1je 5) do (set aversion% I echo! a!) Pause
Results: 12345
Explanation: this example turns on variable delay and uses "!!" Expand the variables so that we get the expected results. What happens if you don't use variable delay? The result is this: ECHO is turned off. ECHO is off. ECHO is off. ECHO is off. ECHO is off. That is, you are not aware of the dynamic changes in the for statement.
Description of batman
Let me make a brief statement:
Set: settin
Local: local (environment variable)
Enable: yes
Delayed: delay
Expansion: extension
Setlocal enabledelayedexpansion is the extension of local environment variable latency
Compare the following two pieces of code:
@ echo off for / l% I in (1set "str=%%i" echo! str!) do (set "str=%%i" echo% str%) pause > nul@echo off&setlocal enabledelayedexpansion for / l% I in (1jie 1je 10) do Pause > nul
The first piece of code shows only 10 lines of "ECHO is off." And the second piece of code correctly displays the 10 lines of numbers of 1mur10 Why is that? Because the str is not defined before the for loop of the two pieces of code, and because the variable delay is not turned on in the first piece of code, the strvalue is always undefined, thus displaying 10 lines.
The second code turns on variable delay, and the value assigned to str in the for loop is passed, so that 10 lines of numbers are displayed correctly, but the str variable here must be written as! strands, which makes no sense, just remember.
What does setlocal enabledelayedexpansion mean?
Yes: set locally to delay extension. In fact, it is: delay variable, full name "delay environment variable extension"
The script is preprocessed before cmd executes the command, and one of the processes is the variable identification process. In this process, if there are two% enclosed variables like% value%, it will be identified, and the corresponding value of the variable will be found, and then the value will be replaced by the variable. The process of replacing the value is called variable extension, and then the command will be executed.
Before explaining, let's look at the differences between a few examples:
Example 1:
Set value=kkkkkkk echo value%
Save this code to a text file with the suffix bat. Then open dos, go to the corresponding directory, and execute this 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 kkkkkkk
Kkkkkkk
The last line is the result, but before the result, there are two sentences, set value=kkkkkkk and echo kkkkkkk, but in the statement, we did not write the statement of echo kkkkkkk, which indicates the substitution of the value of the variable at least up to the sentence of echo% value%. This is the extension of the variable.
So what is the delayed extension of variables?
If you know C++ 's concept of "static variable", you should know that when C++ compiles, it will replace the value of static variable, but this replacement is based on static premise, so it is the same with variable expansion. But what if there is a dynamic situation? In cmd execution, a dynamic situation occurs when a variable is assigned in a for statement, for example:
Example 2:
@ echo off for / l%% I in (1meme 1je 3) do (set knot%% I:: cyclic assignment to k echo% k%% I)
Execute such a script, and the result is as follows:
_ 1
_ 2
_ 3
As a result, these three sentences appeared. _ represents a space
Note: if k is not assigned an initial value, it will be replaced with an empty value.
Example 3:
@ echo off set k=yyy for / l% I in do (set k =% I:: circular assignment to k echo% k%% I)
Results:
Yyy 1
Yyy 2
Yyy 3
Note: if k has an initial value, it will be replaced by yyy. 、
Example 4:
@ echo off setlocal enabledelayedexpansion set k = 3 for / l%% I in (set knot%% I echo% k% I)
Results:
3 1
3 2
3 3
The delay variable is already used here, so why is this still happening? Let's take a look at example 5:
Example 5:
@ echo off setlocal enabledelayedexpansion set k = 3 for / l% I in (set knight% I echo!%% I)
Results:
1 1
2 2
3 3
It turns out that in the delay variable extension, you want to use! To refer to variables.
Thank you for reading! This is the end of this article on "what is the use of setlocal enabledelayedexpansion in batch processing". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.