If I understand you correctly, your external program will generate a list of files. You then want to store this multi-line list to a variable. What do you want to do with the variable once you have it? I assume you want to delete the files, but your question isn't clear on that point, so I'll try to over-answer to cover it.
for /f "delims=" %%a in ('{command that generates your list}') do (
echo Doing stuff to %%a...
echo %%a>>listOfFilesToDelete.txt
set var=%%a
if "%var:~0,7%"="DoNotDelete" copy "%%a" \someArchiveFolder\
del "%%a"
)
This will read each line in your generated list as variable %%a
. It will then do
whatever command(s) you specify. This way, you can run a command on each of the files in the list. In the above code it's
- Printing each line to the console embedded in some text
- Outputting it to a file
- Checking the first 7 characters of the line against a specified string and then copying it to a folder if it matches
- And then deleting it
If you still need to reference each line from your generated list, you can even setup an array-like structure. (See Create list or arrays in Windows Batch)
setlocal EnableDelayedExpansion
:: Capture lines in an 'array'
set /a i=0
for /f "delims=" %%a in ('dir /b') do (
set /a i+=1
set var!i!=%%a
)
:: Loop through the 'array'
for /L %%a in (1,1,%i%) do (
echo Do more stuff with !var%%a!
)
Just like above, this will read each line in your generated list as variable %%a
. It will then set a variable var!i!
equal to the value of the current line. You can then reference each line as var1
, var2
, and so on, or, as the second section shows, you can loop through them all using for /L
. You'll need to get a grasp on working with delayed expansion variables, though.