Batch script-delay environment variable extension
Batch processing (Batch) scripts, which use preprocessing to read by line. In the for loop, the contents of the empty sign after do are processed as one line, so the assignment in the output operation after the assignment in the extension sign is invalid. If you need to make the assignment valid, you need to enable variable delay (full delay environment variable extension). Variables in variable delay cannot use% v% output, you need to use! v! Output.
How to enable variable delay: setlocal enabledelayedexpansion
Example of 1.for loop:
Error:
@ echo off
For% I in (* .txt) do (
Set vault%% I
Echo% v%
)
Pause
Output: echo is off
Correct:
@ echo off
Setlocal enabledelayedexpansion
For% I in (* .txt) do (
Set vault%% I
Echo! v!
)
Pause
Output: xx.txt
two。 An example that shows that the preprocessing output does not match the expected value:
Error:
@ echo off
Set vault 1
Set v=2&echo% v%
Pause
Output: 1
Correct:
@ echo off
Setlocal enabledelayedexpansion
Set vault 1
Set v=2&echo! v!
Pause
Output: 2