Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of delayed expansion in batch file

Can someone give me an example of where a batch script would act differently with or without delayed expansion? Are there any situations where you would NOT want to use delayed expansion? Thanks.

like image 292
Neil Weicher Avatar asked May 11 '12 20:05

Neil Weicher


People also ask

What is delayed expansion in PowerShell?

Delayed Expansion: Only if delayed expansion is on, the command is not in a parenthesized block on either side of a pipe, and the command is not a "naked" batch script (script name without parentheses, CALL, command concatenation, or pipe). Each token for a command is parsed for delayed expansion independently.

Why can't I use delayed expansion with a variable?

What you're trying to do won't work - delayed expansion only changes the variable expansion behavior of a variable inside of a block. It doesn't allow you the aliasing/nesting (for a lack of a better word) that you are attempting.

How do I add a delay/pause/timeout to my batch file?

This topic will teach you one of the many useful things to know in the scripting language, batch file; Adding a delay/pause/timeout to your batch file. The simplest way to make a delay or pause for a certain amount of time, is with the standard command TIMEOUT.

Is it safe to use delayed expansion with complex content?

It is safe, if the delayed expansion is disabled, else the delayed expansion phase is executed after the expansion of the %%a variables. setlocal EnableDelayedExpansion set "var=complex content &<>!" for /F "delims=" %%A in ("!var!") DO ( endlocal set "result=%%A" )


2 Answers

Look at the following examples...

Example 1: The following code DOESN'T use delayed expansion, so the variables in the for loop are expanded only one time. This means that %Count% will always expand to 0 in each iteration of the loop, no matter what we do to it with the set command:

@echo off set COUNT=0  for %%v in (1 2 3 4) do (   set /A COUNT=%COUNT% + 1   echo Count = %COUNT% ) pause 

So this script will output:

Count = 0 Count = 0 Count = 0 Count = 0 

This is not how this loop is supposed to work.

Example 2: On the other hand, if we use delayed expansion, we have the following script, which will run as expected.

setlocal ENABLEDELAYEDEXPANSION set COUNT=0  for %%v in (1 2 3 4) do (   set /A COUNT=!COUNT! + 1   echo Count = !COUNT! )  pause 

and, as expected, it will output:

Count = 1 Count = 2 Count = 3 Count = 4 

When you use the ENABLEDELAYEDEXPANSION, and expand a variable using ! instead of %, the variable is re-expanded each time, and everything works as it's supposed to.

like image 84
Max Avatar answered Oct 10 '22 03:10

Max


I wanted to add a great example on how "EnableDelayedExpansion" (EDE) can be useful outside of the ubiquitous FOR loop examples.

Here is a line of earthquake data that I wish to parse (I call it it 1line.txt)

ak_11574812 2015.04.29.193822 62.9525 -148.8849 1.0 9.5 1 49km S of Cantwell, Alaska

The problem I ran into was that last segment of this line does not always start at the same column number. So I needed to create a flexible SET command that will accurately pluck out the last segment of this line.

ECHO OFF setlocal enableDelayedExpansion set where=72 set /p line=<1line.txt set locate=!line:~%where%,28! echo %locate% 

EDE allows me to place a variable (where) inside another variable (line). EDE will translate the variable bracketed by % first, then process the variable bracketed by ! and (in this case) push out the results into the "locate" variable.

like image 43
Bruce Sinkula Avatar answered Oct 10 '22 05:10

Bruce Sinkula