Get the App
SLTechnology News&Howtos  ›  Servers  › 

Batch script-delay environment variable extension

Shulou Source: shulou.com Published: 2022-06-03 05:24:22 10月03日 Update

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

Tags: Output variable delay mode error loop preprocessing environment script valid one line example full name content expected value state processing expectation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information OPPO Reno Redmi Apple macOS