Environment variable inside an environment variable

  • Thread starter Thread starter murdo
  • Start date Start date
M

murdo

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
 
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 said:
On 24 Feb 2005 03:29:25 -0800, (e-mail address removed) (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
 
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 said:
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
 
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
 
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
 
In said:
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%
 
Back
Top