Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Batch script-delay environment variable extension

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

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

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report