Environment variable inside an environment variable

Discussion in 'Microsoft Windows 2000 CMD Promt' started by murdo, Feb 24, 2005.

  1. murdo

    murdo Guest

    Hi

    I am trying to get the path of the Default user profile (usually
    C:\Documents and Settings\Default User but not always) so I can make a
    script work in all situations.

    I have developed the following script to query the registry using the
    reg.exe (available in the Win2000 resource kit for Win2000 and
    installed as standard in XP)

    FOR /F "tokens=2* delims= " %%A IN ('REG QUERY
    "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
    NT\CurrentVersion\ProfileList" /v ProfilesDirectory') DO SET
    Defaultuser=%%B
    ECHO Defaultuser=%Defaultuser%
    FOR /F "tokens=2* delims= " %%A IN ('REG QUERY
    "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
    NT\CurrentVersion\ProfileList" /v DefaultUserProfile') DO SET
    Defaultuserprofile=%Defaultuser%\%%B
    ECHO Defaultuserprofile=%Defaultuserprofile%

    The result of the Defaultuserprofile echo when this is run in a .bat
    files is %systemdrive%\documents and settings\default user which if
    you try to use creates a %systemdrive% directory in the root of C.
    This is because the environment has already resolved the
    $Defaultuserprofile% variable.

    Can any one help me remove the internal environment varable in
    $Defaultuserprofile% please. It is possible to echo it out but I
    cannot get the result of the echo stored in an environment varibale.

    Thanks

    G
     
    murdo, Feb 24, 2005
    #1
    1. Advertisements

  2. On 24 Feb 2005 03:29:25 -0800, (murdo) wrote:

    >Hi
    >
    >I am trying to get the path of the Default user profile (usually
    >C:\Documents and Settings\Default User but not always) so I can make a
    >script work in all situations.
    >
    >I have developed the following script to query the registry using the
    >reg.exe (available in the Win2000 resource kit for Win2000 and
    >installed as standard in XP)
    >
    >FOR /F "tokens=2* delims= " %%A IN ('REG QUERY
    >"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
    >NT\CurrentVersion\ProfileList" /v ProfilesDirectory') DO SET
    >Defaultuser=%%B
    >ECHO Defaultuser=%Defaultuser%
    >FOR /F "tokens=2* delims= " %%A IN ('REG QUERY
    >"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
    >NT\CurrentVersion\ProfileList" /v DefaultUserProfile') DO SET
    >Defaultuserprofile=%Defaultuser%\%%B
    >ECHO Defaultuserprofile=%Defaultuserprofile%
    >
    >The result of the Defaultuserprofile echo when this is run in a .bat
    >files is %systemdrive%\documents and settings\default user which if
    >you try to use creates a %systemdrive% directory in the root of C.
    >This is because the environment has already resolved the
    >$Defaultuserprofile% variable.
    >
    >Can any one help me remove the internal environment varable in
    >$Defaultuserprofile% please. It is possible to echo it out but I
    >cannot get the result of the echo stored in an environment varibale.
    >
    >Thanks
    >
    >G


    First, try the following:

    @echo off
    setlocal
    FOR /F "tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /v ProfilesDirectory^|FIND "REG_"') DO (
    SET Defaultuser=%%B
    )
    @ECHO Defaultuser=%Defaultuser%
    FOR /F "tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /v DefaultUserProfile^|FIND "REG_"') DO (
    SET Defaultuserprofile="%Defaultuser%\%%B"
    )
    @ECHO Defaultuserprofile=%Defaultuserprofile%
    endlocal

    Second, The %Defaultuserprofile% already exists.

    Third, why would you try to create a %SystemDrive% folder, since %SystemDrive% is C:

    Fourth, MD "%SystemDrive%\Documents and Settings\zzz" works just fine.


    Jerold Schulman
    Windows Server MVP
    JSI, Inc.
    http://www.jsiinc.com
     
    Jerold Schulman, Feb 24, 2005
    #2
    1. Advertisements

  3. "Jerold Schulman" <> wrote:

    > On 24 Feb 2005 03:29:25 -0800, (murdo) wrote:


    [trying to retrieve %DefaultUserProfile% from registry]

    > First, try the following:
    >
    > @echo off
    > setlocal
    > FOR /F "tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /v ProfilesDirectory^|FIND

    "REG_"') DO (
    > SET Defaultuser=%%B
    > )
    > @ECHO Defaultuser=%Defaultuser%
    > FOR /F "tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /v DefaultUserProfile^|FIND

    "REG_"') DO (
    > SET Defaultuserprofile="%Defaultuser%\%%B"
    > )
    > @ECHO Defaultuserprofile=%Defaultuserprofile%
    > endlocal
    >
    > Second, The %Defaultuserprofile% already exists.


    For which versions of Windows does this hold?
    All my Windows 2000 and XP have %UserProfile% and %AllUsersProfile%, but
    no %DefaultUserProfile%!

    There is a simpler solution:
    Set DefaultUserProfile=%AllUsersProfile:\All Users=\Default User%

    This relies but on the fact that both profiles lie beneath the same
    parent directory and have the same suffix (in case of multiple [over]
    installations to the same partition) which I always observed to hold!

    > Third, why would you try to create a %SystemDrive% folder, since %SystemDrive% is C:


    Not necessarily.
    Also not necessarily is "ProfileDir" located on %SystemDrive%.

    > Fourth, MD "%SystemDrive%\Documents and Settings\zzz" works just fine.


    On english standard installations of Windows: yes.
    On all other (language) installations: NO!

    Stefan
     
    Stefan Kanthak, Feb 24, 2005
    #3
  4. murdo

    murdo Guest

    Hi

    The problem is that when the following runs

    FOR /F "tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Microsoft\Windows
    NT\CurrentVersion\ProfileList" /v ProfilesDirectory^|FIND "REG_"') DO
    SET Defaultuser=%%B

    The defaultuser variable is %systemdrive%\documents and settings

    That is an environment variable inside an environment variable, when
    you use this code

    mkdir %defaultuser%\test

    the environment resolves the defaultuser variable to
    %systemdrive%\documents and settings then tries to use it directly
    with out resolving the %systemdrive% environment variable which
    creates the %systemdrive% foldler in the root of C. I do not want
    this folder created, I want the path followed and the folder created.

    I need this for both non-english setups and for PCs that have been
    upgraded or whoose initial format failed mid way though. These last
    two cases whould create the Default user directory something like
    C:\documents and settings\default user.??? where ??? could be the pc
    name, the domain name, the windows directory name or anything else to
    differentiate it from the original default user directory.

    So as I initially asked how can I get %defaultuser% which is
    %systemdrive%\documents and settings\default user to be C:\documents
    and settings\default user?

    I need something like echo %Defaultuserprofile% | set
    Defaultuserprofile2 which creates Defaultuserprofile2 to not have any
    environment variables in it.

    Does this help?

    G
     
    murdo, Feb 25, 2005
    #4
  5. murdo wrote:

    > So as I initially asked how can I get %defaultuser% which is
    > %systemdrive%\documents and settings\default user to be C:\documents
    > and settings\default user?
    >
    > I need something like echo %Defaultuserprofile% | set
    > Defaultuserprofile2 which creates Defaultuserprofile2 to not have any
    > environment variables in it.
    >

    Normally this issue is the other way around ;-)

    call set Defaultuserprofile=%Defaultuserprofile%

    The call forces another level of expansion and should expand %systemdrive%
    to it's current value.

    HTH

    --
    Gruesse Greetings Saludos Saluti Salutations
    Matthias
    ---------+---------+---------+---------+---------+---------+---------+
     
    Matthias Tacke, Feb 25, 2005
    #5
  6. murdo

    murdo Guest

    Hi

    I tried that in the following script

    FOR /F "tokens=2* delims= " %%A IN ('REG QUERY
    "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
    NT\CurrentVersion\ProfileList" /v ProfilesDirectory') DO SET
    Defaultuser=%%B
    ECHO Defaultuser=%Defaultuser%
    FOR /F "tokens=2* delims= " %%A IN ('REG QUERY
    "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
    NT\CurrentVersion\ProfileList" /v DefaultUserProfile') DO SET
    Defaultuserprofile=%Defaultuser%\%%B
    ECHO %Defaultuserprofile%

    set Defaultuserprofile=%Defaultuserprofile%
    ECHO %Defaultuserprofile%

    but the last line stil gives me %SystemDrive%\Documents and
    Settings\Default User

    What I need is away of putting the result of echo | ECHO
    %Defaultuserprofile% into an environment variable. This double
    resolves it can generates C:\Documents and Settings\Default User

    Any ideas?

    G
     
    murdo, Feb 25, 2005
    #6
  7. Tim Hill's book on Windows NT Shell Scripting has the following
    procedure defined in the _mtplib.bat library. It expects the variable
    that has embedded environment variables in it to be named RET, but you
    should be able to modify it to suit your needs.

    it's designed to be called as a procedure:

    CALL :RESOLVE

    ---begin code
    rem
    /////////////////////////////////////////////////////////////////////////
    rem RESOLVE procedure
    rem Fully resolve all indirect variable references in RET variable
    rem
    rem Arguments: RET=value to resolve
    rem
    rem Returns: RET=as passed in, with references resolved
    rem
    :RESOLVE
    if defined TRACE %TRACE% [proc %0 %*]
    :RESOLVELOOP
    if "%RET%"=="" goto :EOF
    set RET1=%RET%
    for /f "tokens=*" %%I in ('echo %RET%') do set RET=%%I
    if not "%RET%"=="%RET1%" goto :RESOLVELOOP
    goto :EOF
    ---end code

    thanks,
    mark
     
    Mark [exmsft], Feb 25, 2005
    #7
  8. murdo

    Mark V Guest

    In microsoft.public.win2000.cmdprompt.admin murdo wrote:

    > Hi
    >
    > I tried that in the following script
    >
    > FOR /F "tokens=2* delims= " %%A IN ('REG QUERY
    > "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
    > NT\CurrentVersion\ProfileList" /v ProfilesDirectory') DO SET
    > Defaultuser=%%B
    > ECHO Defaultuser=%Defaultuser%
    > FOR /F "tokens=2* delims= " %%A IN ('REG QUERY
    > "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
    > NT\CurrentVersion\ProfileList" /v DefaultUserProfile') DO SET
    > Defaultuserprofile=%Defaultuser%\%%B
    > ECHO %Defaultuserprofile%
    >


    Mathias posted "CALL SET" (not SET)...

    > set Defaultuserprofile=%Defaultuserprofile%
    > ECHO %Defaultuserprofile%


    call set Defaultuserprofile=%Defaultuserprofile%
    ECHO %Defaultuserprofile%

    >
    > but the last line stil gives me %SystemDrive%\Documents and
    > Settings\Default User
    >
    > What I need is away of putting the result of echo | ECHO
    > %Defaultuserprofile% into an environment variable. This double
    > resolves it can generates C:\Documents and Settings\Default User
    >
    > Any ideas?
    >
    > G
     
    Mark V, Feb 25, 2005
    #8
    1. Advertisements

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. David Trimboli

    Detecting delayed environment variable expansion

    David Trimboli, Aug 29, 2003, in forum: Microsoft Windows 2000 CMD Promt
    Replies:
    0
    Views:
    925
    David Trimboli
    Aug 29, 2003
  2. Guest

    Set environment variable = output of exe

    Guest, Oct 7, 2003, in forum: Microsoft Windows 2000 CMD Promt
    Replies:
    4
    Views:
    1,964
    Bill Stewart
    Oct 7, 2003
  3. Jimmy

    Export Environment Variable From a Cmd line VB Exe

    Jimmy, May 26, 2004, in forum: Microsoft Windows 2000 CMD Promt
    Replies:
    2
    Views:
    1,586
    Matt Hickman
    May 26, 2004
  4. Claus Koch

    Pipe Output into Environment Variable?

    Claus Koch, Aug 11, 2004, in forum: Microsoft Windows 2000 CMD Promt
    Replies:
    1
    Views:
    14,411
    Matthias Tacke
    Aug 11, 2004
  5. Nunayadam Bidnez

    carriage return in environment variable?

    Nunayadam Bidnez, Aug 29, 2004, in forum: Microsoft Windows 2000 CMD Promt
    Replies:
    1
    Views:
    1,426
    Matthias Tacke
    Aug 29, 2004
  6. Barney

    New Environment Variable?

    Barney, Sep 2, 2004, in forum: Microsoft Windows 2000 CMD Promt
    Replies:
    5
    Views:
    1,165
    Torgeir Bakken \(MVP\)
    Sep 3, 2004
  7. Rich Pasco

    Setting an environment variable with an ampersand but no quotes

    Rich Pasco, Oct 27, 2005, in forum: Microsoft Windows 2000 CMD Promt
    Replies:
    14
    Views:
    4,717
    Rich Pasco
    Oct 29, 2005
  8. kk
    Replies:
    2
    Views:
    3,130
    billious
    Jan 28, 2006
Loading...