I'm trying to accomplish the following ridiculous task:
I have a text file containing a set of fully qualified filesnames. I want to iterate through the file and append each line to a common variable, that can be passed to a command line tool. For example, the file might be:
C:\dir\test.txt
C:\WINDOWS\test2.txt
C:\text3.txt
and I'd like to assign them to some variable 'a' such that:
a = "C:\dir\test.txt C:\WINDOWS\test2.txt C:\text2.txt"
A secondary question is - what is a good batch file reference? I'm finding some stuff in the Windows material, and a lot of home-grown websites, but nothing particularly complete.
What you're after can be done with a FOR /F command.
Here's a good resource I've used many times:
http://www.robvanderwoude.com/batchfiles.php
OS: WINDOWS SERVER 2003
A good book: Windows NT Shell Scripting by Tim Hill. The edition I have was published in 1998 but it is still valid for Windows command programs in Windows 2008.
As for references, SS64.com isn't bad. Rob van der Woude gets linked fairly often, too.
As for your problem, that's easy:
More in-depth explanation:
We're enabling delayed expansion here. This is crucial as otherwise we wouldn't be able to manipulate the list of files within the
for
loop that follows.for /f
iterates over lines in a file, so exactly what we need here. In each loop iteration we append the next line to theLIST
variable. Note the use of!LIST!
instead of the usual%LIST%
. This signals delayed expansion and ensures that the variable gets re-evaluated every time this command is run.Usually
cmd
expands variables to their values as soon as a line is read and parsed. Forcmd
a single line is either a line or everything that counts as a line, which happens to hold true for blocks delimited by parentheses like the one we used here. So forcmd
the complete block is a single statement which gets read and parsed once, regardless of how often the interior of the loop runs.If we would have used
%LIST%
here instead of!LIST!
then the variable would have been replaced immediately by its value (empty at that point) and the loop would have looked like this:Clearly this isn't what we wanted. Delayed expansion makes sure that a variable is expanded only when its value is really needed. In this case when the interior of the loop runs and constructs a list of file names.
Afterwards the variable
%LIST%
or!LIST!
(now it doesn't really matter anymore which to use) contains the list of lines from the file.Funnily enough, the help for the
set
command includes exactly this example for delayed expansion: