Set Variable of folder in specific directory

January 14, 2011 at 21:05:38
Specs: Win 7 Ultimate , 4.17Ghz 8GB ram
I need to know how to set folders in a specified directory to be variables. Then I want to use them in a script. But I know how to do that part.


See More: Set Variable of folder in specific directory

Report •

January 14, 2011 at 21:17:51
I'm guessing you want the names of all folders inside a folder, but not recursively.

for /d %%a in ("t:\he\folder\*") do echo %%a

Report •

January 14, 2011 at 21:24:22
yes I want to make the folder names in a specific directory variables. Does your script do that or am i missing something i just tried it and it just echoed the folder i told it to go into.

Report •

January 14, 2011 at 21:27:57
Did you remember to add the "\*" to the end?

For /d - match wildcards to directories


All folders inside t:\he\folder.

Report •

Related Solutions

January 14, 2011 at 21:31:30
@echo off
for /d %%a in ("C:\users*") do echo %%a


This is my exact code

this is the output
Press any key to continue . . .

Report •

January 14, 2011 at 21:32:51
for /d %%a in ("C:\users\*") do echo %%a

You forgot the backslash that denotes "inside c:\users".

Report •

January 14, 2011 at 21:35:21
ok that worked so now how do i make each directory a variable?

Report •

January 14, 2011 at 21:38:19
Never mind got it. Thank you very much here is the code to do it

SETLOCAL EnableDelayedExpansion
SET Num=1
for /d %%a in ("C:\users\*") do (
SET Var!Num!=%%a
SET /a Num+=1

ECHO %Var1%
ECHO %Var2%
ECHO %Var3%
ECHO %Var4%
ECHO %Var5%
ECHO %Var6%
ECHO %Var7%
ECHO %Var8%


Report •

January 14, 2011 at 21:40:05
Edit: You beat me to it ;)

Technically that does make every name into a variable(%%a) ;)

If possible I would suggest that you expand the loop and use it to do what you want with %%a.

If your heart is set on local environment variables:

Setlocal EnableDelayedExpansion

set cnt=0
for /d %%a in ("C:\users\*") do (
    set /a cnt+=1
    set dir!cnt!=%%a

for /l %%a in (1 1 !cnt!) do echo !dir%%a!

Report •

January 14, 2011 at 21:45:09
no my heart is not set on local environment variables at all it just the way i knew how to do it. i would love to learn a different way.

Report •

January 14, 2011 at 21:48:55
I need individual variables though...

So each user path or name can be put into a file. Its for transferring the profile to a different hard drive.

The other code is way bigger. This will help me out a lot at my job.

Report •

January 14, 2011 at 21:58:07
Edit: You beat me again! Maybe you actually need the variables, after all I don't know all of the details.

Well why don't you post what you doing with the variables and I'll see if I can get it down to the one for loop(for learning purposes of course).

If your doing the same thing with each variable anyway why not add it to the for loop? In most circumstance the for loop variables suffice nicely and can even make the code cleaner. Some things may not work out so well, it really depends on what needs to be done.

Report •

January 14, 2011 at 22:06:43
Well it is a script to get the user accounts of a external drive to the user accounts of the os hard drive. We need this when we reload windows on a customers computer to make sure all their documents, pictures, files etc. are where they should be. So im making a batch file to make this process a whole lot easier ex:

robocopy "c:\Users\Public\Desktop\Old Computer\Users\%username%\Pictures" "C:\Documents and Settings%username%\My Documents\My Pictures" /e /mov

This code is what the final product should do i know that if i use the variables above that it will look a little different. And it will go on for the other folders i tell it to move. Then it will go to the next one and see if there is another profile name and transfer the stuff over to that profile. let me know if your confused but i think you will get what i am trying to do

Report •

January 14, 2011 at 22:30:29
I am a little confused :p

Perhaps I should be content to let you be ;), I'm sorry if I wasted you time it just doesn't sound like something that can easily be wrapped up in a loop. I could be wrong, but making it interactive would make the loop very messy and all that would be gained is lost....

I hope it works out!

Report •

January 14, 2011 at 22:47:10
no you did not waste my time at all. i really appreciate the help. I have one last question though. (You seem very knowledgeable) is there anyway to know how many folders there are so that it doesn't say "ECHO is off." When i list a variable that has not been created yet

if there was only three folders in the folder
if i list a 4th %Var4% then it will give me that above error

i want to keep this process as automated as possible with the least amount of user input

Report •

January 14, 2011 at 23:02:09
You have a counter left over from creating the variables, so you can use that value to determine if there are any variables left.

if %cnt% equ <thenumber>  (
    This one is empty.
    rem exit ?


rem notice the the variable isn't enclosed
if not defined var4  (
    This one is empty.
    rem exit ?

Report •

January 15, 2011 at 03:29:14
Once you set N vars,

for /L %%i in (1 1 !N!) do (
echo V%%i is !V%%i!

Life is too important to be taken seriously.


Report •

Ask Question