You are not logged in.

#1 02 Feb 2015 17:53

Diplodocus
Member
Registered: 09 Sep 2011
Posts: 3

setlocal ENABLEDELAYEDEXPANSION with tunnelling

Hi, I've read many posts on here about DELAYEDEXPANSION but I've not found this particular problem.
I successfully use DELAYEDEXPANSION with TUNNELLING in batch scripts of general form:
set X=oldVal
setlocal ENABLEDELAYEDEXPANSION
FOR /L %%v IN (1,1,3) DO set X=!X! %%v
echo "!X!"  REM gives " 1 2 3"
endlocal&set X=%X%
echo "%X%"  REM gives " 1 2 3"

I am trying to concatenate the code with '&' onto a single line with a view to loading it into a global system variable (eg FORLOOP ) to be executed from scripts via %FORLOOP%
When I try to prototype the single line in a script the setlocal and FOR concatenation works (i.e X builds as a list) but the concatenated endlocal & tunnelling is ignored and the new value of X is lost.

set X=oldVal
setlocal ENABLEDELAYEDEXPANSION&(FOR /L %%v IN (1,1,3) DO (set X=!X! %%v))&echo "!X!"&endlocal&set X=%X%
  REM gives " 1 2 3"
echo "%X%"  REM gives "oldVal"

I've tried many combinations of brackets without success. 
Any suggestions please?

Offline

#2 02 Feb 2015 18:30

DigitalSnow
Member
From: United States
Registered: 27 Dec 2012
Posts: 24

Re: setlocal ENABLEDELAYEDEXPANSION with tunnelling

The situation comes down to how variable expansion works.

Here is a great set of answers by Jeb and DBenham that explain batch scripts:
Answer 1
Answer 2

Essentially, You will not be able to put everything on one line because the %X% at the end will be expanded before the setlocal is event evaluated.

Offline

#3 02 Feb 2015 18:43

Diplodocus
Member
Registered: 09 Sep 2011
Posts: 3

Re: setlocal ENABLEDELAYEDEXPANSION with tunnelling

Thanks DigitalSnow for a prompt response if a shame about the conclusion.
Two excellent links though to some really useful stuff.

Offline

#4 02 Feb 2015 18:53

DigitalSnow
Member
From: United States
Registered: 27 Dec 2012
Posts: 24

Re: setlocal ENABLEDELAYEDEXPANSION with tunnelling

Rather than trying to fit functionality into variables for repeated use, I would recommend using batch routines/functions.  Something like:

rem Usage Example:
set X=oldVal
call :ForLoop
echo %X%
exit /b

rem ForLoop Routine Implementation
:ForLoop
setlocal ENABLEDELAYEDEXPANSION
FOR /L %%V IN (1,1,3) DO set "X=!X! %%V"
echo "!X!"
endlocal & set "X=%X%"
exit /b

This keeps the script much more readable and maintainable.  However, if you must for a mental exercise put it all in a variable for use, something like this should work (not tested), though the 'call set' methodology is not recommended and has poor performance:

set "FORLOOP=FOR /L %%V IN (1,1,3) DO call set X=%%X%% %%V"
%FORLOOP%

Offline

#5 03 Feb 2015 18:40

Diplodocus
Member
Registered: 09 Sep 2011
Posts: 3

Re: setlocal ENABLEDELAYEDEXPANSION with tunnelling

Thanks for the suggestions DigitalSnow.  Tried the load variable one and it works fine which suits me perfectly.
What I'm actually trying to do is not really a loop it's just using the features of the FOR command so performance isn't an issue.
I used the 1 2 3 scenario in my post as the simplest illustration of the issue that I could think of.
b.t.w I take your point about readability but my stuff is all for home consumption so I tend towards brevity in the many batch files I seem to have acquired.

Offline

Board footer

Powered by