Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

get reg_expand_sz value from system registry and put it to variable

306 views
Skip to first unread message

alexsupra

unread,
Nov 18, 2010, 2:58:09 PM11/18/10
to
it's necessary to get shell enviroment values and put them into
variables.
for example:
for /f "tokens=3*" %%a in ('reg query "hkcu\software\microsoft\windows
\currentversion\explorer\shell folders" /s ^|find /i "Local AppData"')
do set "localappdata=%%b"
for /f "tokens=2*" %%a in ('reg query "hkcu\software\microsoft\windows
\currentversion\explorer\shell folders" /s ^|find /i "Desktop"') do
set "desktop=%%b"
in this case the values in vars equal full directory path i.e. for my
system
%localappdata% = C:\docs\admin\local settings\Application Data
%desktop% = C:\docs\admin\desktop
but there are some other values in system registry that stored not in
REG_SZ but in REG_EXPAND_SZ type.
for example:
for /f "tokens=2*" %%a in ('reg query "hklm\software\microsoft\windows
nt\currentversion\profilelist" /s ^|find /i "ProfilesDirectory"') do
set "docs=%%b"
than the result is the following
%docs% = %systemdrive%\docs
actually the path is correct but it contains not expanded variable
written to other one.
the question is: how to modify the string
for /f "tokens=2*" %%a in ('reg query "hklm\software\microsoft\windows
nt\currentversion\profilelist" /s ^|find /i "ProfilesDirectory"') do
set "docs=%%b"
to make it write all variables in expanded form?

\Rems

unread,
Nov 18, 2010, 5:23:27 PM11/18/10
to

Instead of: set "docs=%%b"
use: call set "docs=%%b"


sample:
------------------------------------------------

@echo off

Set "RegKey=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion
\ProfileList"
rem Set "RegItem=ProfilesDirectory"

Set "RegItem=ProfileImagePath" & Set "strUser=Administrator"

Set strPttrn=/riec:"%strUser%" /ric:"%strUser%\.[0-9]*$"
for /f "tokens=2* delims= " %%a in (
'reg.exe query "%RegKey%" /s ^|(
find.exe /i "%RegItem%" ^|findstr.exe %strPttrn%^)'
) do Call Set "UserProfilePath=%%b"

:: test
set UserProfilePath & pause

------------------------------------------------

\Rems

foxidrive

unread,
Nov 18, 2010, 7:37:56 PM11/18/10
to
On 19/11/2010 09:23, \Rems wrote:
>> for example:
>> for /f "tokens=2*" %%a in ('reg query "hklm\software\microsoft\windows
>> nt\currentversion\profilelist" /s ^|find /i "ProfilesDirectory"') do
>> set "docs=%%b"

>> than the result is the following
>> %docs% = %systemdrive%\docs

>> actually the path is correct but it contains not expanded variable
>

> Instead of: set "docs=%%b"
> use: call set "docs=%%b"
>
>
> sample:
> ------------------------------------------------
>
> @echo off
>
> Set "RegKey=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion
> \ProfileList"
> rem Set "RegItem=ProfilesDirectory"
>
> Set "RegItem=ProfileImagePath"& Set "strUser=Administrator"
>
> Set strPttrn=/riec:"%strUser%" /ric:"%strUser%\.[0-9]*$"
> for /f "tokens=2* delims= " %%a in (
> 'reg.exe query "%RegKey%" /s ^|(
> find.exe /i "%RegItem%" ^|findstr.exe %strPttrn%^)'
> ) do Call Set "UserProfilePath=%%b"
>
> :: test
> set UserProfilePath& pause
>
> ------------------------------------------------


This shows that adding a separate statement after the set command will
help. It also uses call.


@echo off
set docs=%%systemdrive%%\docs
echo %docs%
call set "docs=%docs%"
echo %docs%
pause

--
Regards,
Mic

alexsupra

unread,
Nov 20, 2010, 8:27:30 AM11/20/10
to

thank you bro.
do you know is that possible somehow without using of "call". i mean
does alternative variant exist?
p.s. if it is interesting i can explain why its impossible to use
calling in many cases...

alexsupra

unread,
Nov 20, 2010, 11:03:30 AM11/20/10
to

\Rems

unread,
Nov 20, 2010, 12:03:22 PM11/20/10
to
> calling in many cases...- Hide quoted text -
>
> - Show quoted text -

Just curious why you could not use CALL SET in the batch.
The batch is CALLing an *internal command*, it is not calling an
external batch here or a subroutine. The CALL does one substitution of
the variable (use CALL CALL... for multiple substitutions).
You can use "CALL SET" when Command Extensions are enabled <= is
enabled by Default.
else, use SETLOCAL ENABLEEXTENSIONS in the batch to enable it.
Command Extensions are only available on Windows machines running the
32 bit command line (cmd.exe)

Actually there is a workaround,

<...cut...>

For /f "delims=" %%* in (
'echo.%docs% ^|find "%docs:~-1%"') Do set "docs=%%*"

echo %docs% & pause


\Rems

0 new messages