2011-02-04

Batch scripting: Reading paths from registry

Reading paths from registry can be extremely useful if you are making your own automated installation or something similar. Getting the path from registry removes the need for manual entry of the path.

As there exist a few differences when running commands from batch file and entering them directly into command window, I'll explain how to do it with a batch file.

Let's presume we have JDownloader installed on our computer.
Registry key "HKEY_LOCAL_MACHINE\SOFTWARE\JDownloader" contains a string "Path" which has an installation path stored in it.

We can store its value in a temporary variable "installpath" using following command:

for /f "tokens=2*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\JDownloader" /v "Path"') do set installpath=%%b)

Our current command will only work on Windows with 32-bit architecture, because on Windows with 64-bit architecture that same registry key is stored in a different location. That location is: "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\JDownloader"

The reason for this is because 32-bit applications on 64-bit Windows put their registry keys under Wow6432Node branch. Using the fact that 64-bit Windows have SysWOW64 folder under Windows folder, we'll make our command architecture independent by modifying it like this:

if exist "%windir%\SysWOW64" (
    for /f "tokens=2*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\JDownloader" /v "Path"') do set installpath=%%b
) else (
    for /f "tokens=2*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\JDownloader" /v "Path"') do set installpath=%%b
)

If registry string which value you are getting has multiple words in its name, value of tokens should be increased by number of spaces inside the name. For example, if string is named "Install Path" you must change tokens to "tokens=3*".

If path you are reading from a registry key is stored in default string, there exists another problem. On Windows XP and earlier, "reg query" command returns the name of default string as "(NO NAME)", while on Windows Vista and later returns as "(Default)".

Because of this change, value of tokens is different as there is one additional space on Windows XP and earlier. If JDownloader had its path stored in default string, to make our command independent of Windows version, we would have to modify it like this:

for /f "tokens=2 delims=[]" %%x in ('ver') do set winver=%%x
set winver=%winver:~8,1%

if %winver% geq 6 (
    if exist "%windir%\SysWOW64" (
        for /f "tokens=2*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\JDownloader" /ve') do set installpath=%%b
     ) else (
        for /f "tokens=2*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\JDownloader" /ve') do set installpath=%%b
     )
) else (
    if exist "%windir%\SysWOW64" (
        for /f "tokens=3*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\JDownloader" /ve') do set installpath=%%b
    ) else (
        for /f "tokens=3*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\JDownloader" /ve') do set installpath=%%b
    )
)

There is one other thing I would like to mention, which doesn't effect our way of getting a path, but is good to be aware of when working with "reg query".

As there is a difference in a name of the default value, there also exists a difference in a way columns are being separated. While on Windows XP and earlier tabulator character has been used , on Windows Vista and later, it has been changed to four space characters.

That should be all. :)