Question about Windows Shell and Environment Variables

Status
Not open for further replies.

LookinAround

Posts: 6,429   +186
Question:

I want the syntax that allows one Windows shell variable expand within another variable. How do i do it? Let me functionally explain what i want

1) Windows defines a shell variable USERPROFILE for each user logon shell (and that's what i want to reference indirectly in a commanded prompt window)
2) I essentially want to define another (shell or environment?) variable named out= something like "%USERPROFILE%\Desktop\outfile.txt" such that all i need do in a command prompt window is type
Code:
[b]echo hello > %out%[/b]
and then see a file appear on my desktop named outfile.txt containing the word hello

What's the syntax????
 
Thanks for the quick reply. But i want to tweak the answer such that
1) i type/run the command any time within an open command prompt
2) i want to minimize key strokes. That's why i'm really looking for a way to represent the entire current USERPROFILE path / Desktop/outfile.txt (filename hard coded is fine since it appears on a different desktop per the user logon) but all i have to type under any user login is something like %out% or whatever

e.g echo hello > %out%
 
I had a think about setting up %out% to equal %currentuserprofile%\Desktop\outfile.txt in Environment Variables in System; but I think you would be better off calling a batch file each time, called "out.bat" or something, and out.bat can reside in the normal Windows path location like C:\Windows or somewhere.

Yep batch file preferred
 
Thanks. will probably go down the batch route.

i also had gone the path of trying to define out in my Environment variables.. but seemed the "variable-within-a-variable" (i.e. using USERPROFILE inside of out definition) wouldn't expand correctly on reference so failed :dead:

In any case, thanks for having a look! :)
 
Yes good point, I hadn't actually checked a variable within a variable issue
But if this is to be only used on one computer you could actually write the entire path and not use a Profile variable at all.

But this is why again a batch file would be better, because then you could use it on any computer :grinthumb
 
hmm; I've set
Code:
JAVAHOME=C:\Program Files\Java
JAVAV=jre1.6.0_xx
JAVARTE=%JAVAHOME%\%JAVAV%\bin
so I can control the %PATH% to Java with ...;%JAVARTE%;...

works for me
 
A Thank you! to jobeard

And both a "AH-HA!" and a "Duh, of course!" to me!

When i read jobeard's post, it occured to me

1) Windows' shell variables are entirely "order sensitive". A variable must be defined before you can reference it
2) I had defined variable out="%USERPROFILE%\DESKTOP\outfile.txt"
3) My problem was that i defined it in System Environment Variables and not User Environment Variables
4) USERPROFILE doesn't exist until the user logs on so Windows hasn't defined it yet for usage in System Variables!
5) I moved it to User Variables, tested it and it now works as i wanted. Thank you all!
 
Status
Not open for further replies.
Back