I am working on a "for loop that finds all the drives on a system that match a given criteria and am trying to accumulate the drive letters into a list(string) which I can then pipe to a command which will do something to those drives. I do not use the command prompt every day, so please keep explanations simple. here is the code I have so far;

for %a in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (IF NOT EXIST %a:\evac.txt If EXIST %a:\ SET "targetDR=%targetDR %a:" )

The "SET "targetDR=%targetDR %a:" )" is not working as it should. I want the code to accumulate like so;
"C:\ D:\ E:\ F:\" provided the drives match the if statements I have preceding the code. "echo %targetDR%" yields "%targetDR H:" which is incorrect. I will NOT be running this code in a batch file, I will be running it directly from the command prompt. Also, I am not very familiar with variable operations in the command prompt, because in some code I see differing variable declarations like the following; %var%, %var, !var! ect...

I have figured out a couple things concerning my previous post. The first, and possably most important is that the preceding loop needs to have Delayed Environment Variable Expansion enabled. Apparently MSDOS has problems if there are refrences to the same variable more than once on the same line, and Delayed Environment Variable Expansion is the key to fixing these problems. I have also found out that if there is no text assigned to the accumulator variable, then the first time through the loop the variable assigns %targetDR% to the variable. To avoid this assign some text to the variable first, then delete the first character like shown in some of the code I provide below. Also, to take advantage of Delayed Environment Variable Expansion you need to use exclamation(!) marks instead of percent symbols(%). The following code executes directly in the command shell, and needs modified if you want to execute it in a batch file.

REM SET THE FLAG FOR ENABLE DELAYED ENVIRONMENT VARIABLE EXPANSION
REM FROM WHAT I UNDERSTAND THERE ARE INDIRECTION PROBLEMS WHEN NOT 
REM SET AND THE VARIABLE IS CALLED MULTIPLE TIMES ON THE SAME LINE
cmd.exe /V:ON
REM IF SOME TEXT IS NOT ASSIGNED TO THIS VARIABLE THE LOOP WILL FAIL AND
REM CAUSE THE VARIABLE NAME TO BE ASSIGNED TO THE BEGINNING OF THE LIST
Set targetDr=*
REM THE FOR LOOP THAT SEEKS OUT SUITABLE DRIVES, HAS SOME NIFTY INDIRECTION
REM OPERATORS "!" WHICH ARE USED WHEN THE VARIABLE IS CALLED MULTIPLE 
REM TIMES IN THE SAME LINE SO IT CAN TAKE ADVANTAGE OF THE ENABLED DELAYED
REM EXPANSION WE SET EARLIER
for %a in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do ( IF NOT EXIST %a:\evac.txt If EXIST %a:\ SET targetDr=!targetDr! %a: )
REM REMOVE THE SINGLE CHARACTER OF TEXT WE ASSIGNED TO THE 
REM LIST VARIABLE EARLIER
Set targetDr=%targetDr:~1%
REM SHOW ME THE OUTPUT
echo %targetDr%

The entire preceding code block can be copied and paisted into the command prompt, and it will execute line by line.

This question has already been answered. Start a new discussion instead.