1.

Solve : Use a command to set a variable?

Answer»

Hello.

I am trying to come up with a way to set an environmental variable based on a folder name. If I run Code: [Select]dir /b it outputs the name that I WANT, but I can't figure out how to GET that into a Code: [Select]set name= command.

The reason I want to do this is because I need a script that when I run as an administrator it can automatically find the user that the computer belongs to. I have a login script that automatically creates the folder, now I just need a way to use it.

Any help on this would be greatly appreciated.what OS are you using? i would try the set /p name= commandI'm using Windows XP.

When I use /p it sets the variable to "dir /b" not the resultant name of "dir /b"hmm.... i know theres a way to do this.... i'm testing it a bit
im trying something like this

-----------------------------------------------------------------------------------------------------------------------------------------
dir /b >> "C:\Documents and Settings\coolname.txt"
type "C:\Documents and Settings\coolname.txt"
----------------------------------------------------------------------------------------------------------------------------------------
the problem is i still can't get it to work, beside manually comping and pasting the name into the set command.Could the "For" command be used to get this? I have NEVER actually used the For command, but I have seen people use it to PUT Date and Time into variables.

Well I'm going to go do some reading on "For" and see if that will do what I want it to.good idea. im reading about the FOR command, man does it get complicated.You want to get a folder name into a variable? Please give more details like

is the folder under another folder? (names?)

is it the only one under that folder?

does dir /b output the folder name and nothing ELSE?

Surely if you were looking for a folder name then you'd use dir /b /ad

Answer all of these questions please & I I'll try to get back to you

The folder is located in C:\User
There is only one folder in the User folder
The Dir /b give only the name of the folder I want to make a variable
There are no files or anything else, so the /ad option is not needed.

I hope that helps.use this comands inside a bat file

for /f %%i in ('dir /b') do set name=%%i
echo %name%

i hope its usful for u

u should put dir /b C:\User
in stead of dir /bQuote from: .bat_man on April 08, 2008, 07:29:24 AM

use this comands inside a bat file

for /f %%i in ('dir /b') do set name=%%i
echo %name%

i hope its usful for u

That's perfect! It did exactly what I needed it to, Thanks!


Discussion

No Comment Found