1

I am trying to make a batch script that will take the names of all sub-folders in a directory as variables for use elsewhere.

The section of code that is confusing me is:

 for /d %%D in (C:\Users\tcsupport\Desktop\Test\*) ^
 do (SET test=%%D
 echo %%D
 echo %test%
 echo %test:~32%)

The sub folders in the directory 'Test' are: 1, 2#, 3, ads, and ghf.
These are just random names for testing.

The output I am getting is:

C:\Users\tcsupport\Desktop\Test\1  
C:\Users\tcsupport\Desktop\Test\ghf  
ghf  
C:\Users\tcsupport\Desktop\Test\2#  
C:\Users\tcsupport\Desktop\Test\ghf  
ghf  
C:\Users\tcsupport\Desktop\Test\3  
C:\Users\tcsupport\Desktop\Test\ghf  
ghf  
C:\Users\tcsupport\Desktop\Test\ads  
C:\Users\tcsupport\Desktop\Test\ghf  
ghf  
C:\Users\tcsupport\Desktop\Test\ghf  
C:\Users\tcsupport\Desktop\Test\ghf  
ghf 

but I was expecting:

C:\Users\tcsupport\Desktop\Test\1  
C:\Users\tcsupport\Desktop\Test\1  
1  
C:\Users\tcsupport\Desktop\Test\2#  
C:\Users\tcsupport\Desktop\Test\2#  
2#  
C:\Users\tcsupport\Desktop\Test\3  
C:\Users\tcsupport\Desktop\Test\3  
3  
C:\Users\tcsupport\Desktop\Test\ads  
C:\Users\tcsupport\Desktop\Test\ads  
ads  
C:\Users\tcsupport\Desktop\Test\ghf  
C:\Users\tcsupport\Desktop\Test\ghf  
ghf  

Can anyone explain why this is happening or think of a way around it?

2

1 Answer 1

1

Looking at the information in the links provided by Techie007 i found my answer.

Delayed Expansion was the issue. Changing the code to the following solved the problem.

 Setlocal EnableDelayedExpansion  
 for /d %%D in
 (C:\Users\tcsupport\Desktop\Test\*) ^  
 do (SET test=%%D  
 echo %%D  
 echo !test!  
 echo !test:~32!)

Sorry about the question duplication.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .