Reading values from Windows registry

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
milos78
Posts: 6
Joined: 28 Jan 2011 14:02

Reading values from Windows registry

#1 Post by milos78 » 01 Sep 2011 07:35

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.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Reading values from Windows registry

#2 Post by aGerman » 01 Sep 2011 11:18

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

milos78
Posts: 6
Joined: 28 Jan 2011 14:02

Re: Reading values from Windows registry

#3 Post by milos78 » 04 Sep 2011 06:37

Thank you very much!

Works perfectly.

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

Post Reply