Page 1 of 1

Reading values from Windows registry

Posted: 01 Sep 2011 07:35
by milos78
Hi,

I want to read a Environment Variables values from registry.

Example, a path to desktop in English Windows is:
%USERPROFILE%\Desktop

but in French windows it is:
%USERPROFILE%\Bureau

That is why I need to use the path in the batch command.
%USERPROFILE%\"what i read from registry value"

The key that keep those values is:
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders]

Please help on this example.

Re: Reading values from Windows registry

Posted: 01 Sep 2011 11:18
by aGerman
It's tricky since M$ removed the tab characters and replaced them by a few space characters in Vista and Win7.
I checked this key on my Win7 machine and found that:
- all data types are REG_EXPAND_SZ
- the value names contain max. two words

That code works for me

Code: Select all

@echo off &setlocal enabledelayedexpansion
for /f "tokens=2 delims=:" %%i in ('chcp') do set /a oemcp=%%~ni
>nul chcp 1252
for /f "delims=" %%a in (
  'reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"^|find /i "REG_EXPAND_SZ"'
) do (
  set "line=%%a"
  for /f "tokens=1* delims=?" %%b in ("!line:REG_EXPAND_SZ=?!") do (
    for /f "tokens=1,2" %%e in ("%%b") do (
      for /f "tokens=*" %%g in ("%%c") do (
        if "%%f"=="" (
          call set "%%e=%%g"
        ) else (
          call set "%%e %%f=%%g"
        )
      )
    )
  )
  set "line="
)
>nul chcp %oemcp%
set "oemcp="


echo ***check whether all shell folders are part of the environment now***
set
echo(
echo desktop folder is "%Desktop%"
pause

Regards
aGerman

Re: Reading values from Windows registry

Posted: 04 Sep 2011 06:37
by milos78
Thank you very much!

Works perfectly.

Tested on Win XP, English and French.
Win 7 English 32-bit and 64-bit.