TechSpot

Question about Windows Shell and Environment Variables

By LookinAround
Apr 16, 2009
  1. 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????
     
  2. kimsland

    kimsland Ex-TechSpotter Posts: 14,524

    Start --> Run--> cmd /c echo hello >Desktop\outfile.txt < ok>
     
  3. LookinAround

    LookinAround Ex Tech Spotter Topic Starter Posts: 6,491   +183

    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%
     
  4. kimsland

    kimsland Ex-TechSpotter Posts: 14,524

    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
     
  5. LookinAround

    LookinAround Ex Tech Spotter Topic Starter Posts: 6,491   +183

    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! :)
     
  6. kimsland

    kimsland Ex-TechSpotter Posts: 14,524

    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
     
  7. jobeard

    jobeard TS Ambassador Posts: 9,333   +622

    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
     
  8. LookinAround

    LookinAround Ex Tech Spotter Topic Starter Posts: 6,491   +183

    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!
     
Topic Status:
Not open for further replies.

Similar Topics

Add New Comment

You need to be a member to leave a comment. Join thousands of tech enthusiasts and participate.
TechSpot Account You may also...