You are not logged in.
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
Offline
Thanks DigitalSnow for a prompt response if a shame about the conclusion.
Two excellent links though to some really useful stuff.
Offline
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
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