Dynamically assigned environmental variables

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
batchfileman
Posts: 16
Joined: 07 Jun 2010 10:27

Dynamically assigned environmental variables

#1 Post by batchfileman » 07 Jun 2010 10:31

I am looking to write a batch file that will take the usernames out of the directory "C:/Documents and Settings/****" and store them in environmental variables.
I then want to be able to then call each of these variables individually. For example I may want # 50 but not 39-43. I am designing the logic behind that but cannot get the variables to store the way I want. Any suggestions?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Dynamically assigned environmental variables

#2 Post by aGerman » 07 Jun 2010 10:49

Try this

Code: Select all

@echo off &setlocal
pushd "C:\Documents and Settings"
for /f "delims=: tokens=1*" %%a in ('dir /ad /b^|findstr /n .') do set "userVar%%a=%%b"
popd

:: display all
set userVar
pause


Regards
aGerman

batchfileman
Posts: 16
Joined: 07 Jun 2010 10:27

Re: Dynamically assigned environmental variables

#3 Post by batchfileman » 07 Jun 2010 12:23

So this works to put each of the usernames into a variable. Now why can I not reference them.
For example I cannot "echo userVar7" and get the seventh entry.

batchfileman
Posts: 16
Joined: 07 Jun 2010 10:27

Re: Dynamically assigned environmental variables

#4 Post by batchfileman » 07 Jun 2010 12:25

DOH! I'm a butt. echo %userVar7%. All set here. You are wonderful. Thank you my german friend.

batchfileman
Posts: 16
Joined: 07 Jun 2010 10:27

Re: Dynamically assigned environmental variables

#5 Post by batchfileman » 07 Jun 2010 12:49

What about testing the variables dynamically. For example I am on a domain network. I am looking specifically to remove an icon that has been placed on individual users desktops. I do not really care about the administrator, localservice, setup etc... users. However some machines only have one user and some have hundreds. Any good way to do that?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Dynamically assigned environmental variables

#6 Post by aGerman » 07 Jun 2010 13:03

No, sorry. I've no idea how to do this remotely.

Regards
aGerman

Post Reply