I unexpectedly ran into this issue. Thinking about how most modern programming languages work in relation to setting of variables, I forgot how older languages work.
In today’s languages, you can simply do something like:
int myVar = 1;
for(int i; i<4; i++)
myVar = myVar + i;
However, that requires the variable of “myVar” to be evaluated/expanded during the assignment. This is something we take for granted. If “myVar” is not evaluated/expanded during the execution, then the result will not take the current value of “myVar” into consideration during the assignment/execution.
In DOS, this requires that we delay the expansion until after the execution (instead of before the execution which is default). Take these commands as an example:
set FILES=
for /f %%a IN (‘dir /b *.txt’) do set FILES=%FILES% %%a
echo %FILES%
In a directory with 3 files that match the *.txt pattern, you’d expect the %FILES% variable would be set to “file1.txt file2.txt file3.txt”. However, the result will be ” file3.txt”.
We need to delay the execution/expansion of “%FILES%” until we assign the FILES variable, so our syntax changes to:
SETLOCAL ENABLEDELAYEDEXPANSION
set FILES=
for /f %%a IN (‘dir /b *.txt’) do set FILES=!FILES! %%a
echo %FILES%
You’ll noticed a new declaration in our DOS batch file and that we’ve changed the variable to use exclamation marks instead of the standard percent signs. This will result in our expected output of “file1.txt file2.txt file3.txt”.
Resources:
Hi,
I don’t seem to get the Eable Delayed Expansion mode on. I tried the setlocal EableDelayedExpansion command, and also to run the script with cmd /V:ON /C script..
I am running in a DOS box of Windows XP. Does that make a difference?
Basically, I am trying to get a list of filenames on one line- The reason:
for /F %%A ….. do xcopy /D /Y %%A destination
is invoking xcopy a lot of times. The destination being a non local filesystem does not make things better..
I wanted to get to
xcopy /D /Y file1 file2 file3 destination
Any ideas?
Rob.
I have no programming backgroud but trying to create a dos batch file to run a program locally , which will generate multiples cpout.* files (ex. cpout.001, cpout.002 and etc.). I need to able to rename each cpout.*** files to different name consecutively. The last command ask and let me rename file just once but I can not find the renamed files anywhere. I am looking for even any utility program that will do that. Below is the batch file I created for your information. Appreciate any help
COPY \\FILEMI1\CP3\PARTL.PTS C:\CP1\PARTS
CP.EXE
SET /P SEQNAME=ENTER SEQUENCE NAME:
pause
THANK YOU!
THANK YOU to the infinity power! I’ve been chasing this problem for 4 hours. I was finally able to isolate the problem to the inability to access variables within a FOR loop that were set within that FOR loop, like so:
:: The code below does NOT work!
FOR %%A IN (c:\test\*) do (
set value=something
echo %value%
)
Could NOT figure out why this would not work! Blows my mind that I have to do some esoteric weirdness to make this simple code have sane behavior!
:: This works!
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%A IN (c:\test\*) do (
set value=something
echo !value!
)
So again, thank you! I would have wasted a lot more than 4 hours on this problem had you not published the solution.
Thank you very much!
I was luckier then Lynwood and just chased this for half an hour, but still way to much time wasted!
Bobby