How to get registry key value from script package?

I need to use the REG QUERY command to view the key value and set the result to a variable using this command:

FOR /F "tokens=2* delims= " %%A IN ('REG QUERY "KeyName" /v ValueName') DO SET Variable=%%B 

But if the key does not exist, I get an error message displayed in the console. I need to hide this error! I tried putting 2> nul after the command to stop stderr, but this works if I only call the command:

 REG QUERY "KeyName" /v ValueName 2>nul 

If I put it in the FOR command like this:

 FOR /F "tokens=2* delims= " %%A IN ('REG QUERY "KeyName" /v ValueName') DO SET Variable=%%B 2>nul 

Error is displayed. So does anyone know how to hide the error? Or maybe the other team also sees if the key exists or not?

thank

PS: I am using Windows XP

+46
batch-file registry
Jan 15 '09 at 0:05
source share
16 answers

This works for me:

 @echo OFF setlocal ENABLEEXTENSIONS set KEY_NAME="HKEY_CURRENT_USER\Software\Microsoft\Command Processor" set VALUE_NAME=DefaultColor FOR /F "usebackq skip=4 tokens=1-3" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO ( set ValueName=%%A set ValueType=%%B set ValueValue=%%C ) if defined ValueName ( @echo Value Name = %ValueName% @echo Value Type = %ValueType% @echo Value Value = %ValueValue% ) else ( @echo %KEY_NAME%\%VALUE_NAME% not found. ) 

usebackq necessary because the REG QUERY command uses double quotes.

skip=4 ignores all output, except for a string that has a name, type, and value if it exists.

2^>nul prevents error text from appearing. ^ is an escape character that allows you to put > in a for command.

When I run the script above as indicated, I get this output:

 Value Name = DefaultColor Value Type = REG_DWORD Value Value = 0x0 

If I change the value of VALUE_NAME to BogusValue , then I get the following:

 "HKEY_CURRENT_USER\Software\Microsoft\Command Processor"\BogusValue not found. 
+45
Jan 15 '09 at 1:23
source share

This work for me with a variable that contains spaces in Windows 7:

 FOR /F "usebackq tokens=3*" %%A IN (`REG QUERY "HKEY_LOCAL_MACHINE\Software\SomeAPP" /v ValueName`) DO ( set appdir=%%A %%B ) ECHO %appdir% 

Variable A contains all the data before the first space, B is part of the break of ValueName (including extra spaces), therefore appdir = ValueName

+19
Dec 02
source share

Based on the tryToBeClever solution (which I also encountered and fixed with trial and error before finding it), I also suggest passing the result of the output of reg query through find to filter out unwanted strings due to inconsistency ! REG.EXE VERSION xy ! REG.EXE VERSION xy . find filtering and token tokens also allow us to precisely select what we want (usually a value). Quotation marks have also been added to avoid unexpected results with key / value names containing spaces.

The end result proposed when we are only interested in choosing a value:

 @echo off setlocal ENABLEEXTENSIONS set KEY_NAME=HKCU\Software\Microsoft\Command Processor set VALUE_NAME=DefaultColor for /F "usebackq tokens=3" %%A IN (`reg query "%KEY_NAME%" /v "%VALUE_NAME%" 2^>nul ^| find "%VALUE_NAME%"`) do ( echo %%A ) 

A potential obstacle to using find is that the error level set by reg when errors occur is now getting confused, so you should use this approach only for keys that are known to exist and / or after a previous check.

A small additional optimization (add skip=1 to avoid processing the first line of output) can be performed in cases where the key name also contains the value name (as is the case with HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion and CurrentVersion ) but eliminates more flexibility, so they should be used only in specific cases of use.

+7
Dec 22 '11 at 14:33
source share

For Windows 7 (Professional, 64-bit - I can't speak for others), I see that REG no longer spits out

 ! REG.EXE VERSION 3.0 

like in XP. Therefore, the foregoing needs to be modified in order to use

 skip=2 

instead of 4 - which makes things messy if you want your script to be portable. Although it is much more difficult and complicated, a WMIC solution could be better.

+4
Feb 17 '11 at 13:19
source share

For some reason, Patrick Kuff's code is not working on my system (Windows 7), probably due to a tryToBeClever comment. Modifying this did a little trick:

 @echo OFF setlocal ENABLEEXTENSIONS set KEY_NAME=HKEY_CURRENT_USER\Software\Microsoft\Command Processor set VALUE_NAME=DefaultColor FOR /F "tokens=1-3" %%A IN ('REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul') DO ( set ValueName=%%A set ValueType=%%B set ValueValue=%%C ) if defined ValueName ( @echo Value Name = %ValueName% @echo Value Type = %ValueType% @echo Value Value = %ValueValue% ) else ( @echo %KEY_NAME%\%VALUE_NAME% not found. ) 
+4
Oct 31 '11 at 23:23
source share
 @echo off setlocal ENABLEEXTENSIONS set KEY_NAME=HKLM\SOFTWARE\Wow6432Node\Acme Software Inc\Common set VALUE_NAME=InstallDir FOR /F "tokens=2*" %%A IN ('REG.exe query "%KEY_NAME%" /v "%VALUE_NAME%"') DO (set pInstallDir=%%B) echo %pInstallDir% 

This works for me in Win7, where the key has a space, and the value also has a space. Thus, saving the above in c: \ temp as test.bat, open the cmd window and run it.

C: \ TEMP> test

C: \ Program Files (x86) \ acme Software Inc \ APP \

+4
04 Oct
source share

This works if the value contains a space:

 FOR /F "skip=2 tokens=1,2*" %%A IN ('REG QUERY "%KEY_NAME%" /v "%VALUE_NAME%" 2^>nul') DO ( set ValueName=%%A set ValueType=%%B set ValueValue=%%C ) if defined ValueName ( echo Value Name = %ValueName% echo Value Type = %ValueType% echo Value Value = %ValueValue% ) else ( @echo "%KEY_NAME%"\"%VALUE_NAME%" not found. ) 
+4
Nov 28 '12 at 17:21
source share

Great level of solutions here.

My little grain of salt as @Patrick Cuff's solution doesn't work out of the box; I had 2 problems.

  • I am using Windows 7 => changed to "skip = 2"
  • The value of the registry value contained a space. Value Value = C:\Program Files\...

Here is the solution I found: taking 4 tokens and setting ValueValue to %% C and %% D. (Thanks @Ivan!)

 setlocal ENABLEEXTENSIONS set KEY_NAME="HKEY_CURRENT_USER\Software\Microsoft\Command Processor" set VALUE_NAME=DefaultColor FOR /F "usebackq skip=2 tokens=1-4" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO ( set ValueName=%%A set ValueType=%%B set ValueValue=%%C %%D ) if defined ValueName ( @echo Value Name = %ValueName% @echo Value Type = %ValueType% @echo Value Value = %ValueValue% ) else ( @echo "%KEY_NAME:"=%\%VALUE_NAME%" not found. ) 
+4
Dec 11 '14 at 12:12
source share

I understand this is an old question, but this one-liner interface is pretty much like your original attempt with a few additions. It works with paths, including spaces, and works in both XP and Windows 7, even if the key is not found (and hides the error). % fn% will be empty if the key does not exist. This example displays the current desktop file name:

 for /f "tokens=2*" %%a in ('reg query "HKEY_CURRENT_USER\Control Panel\Desktop" /v Wallpaper 2^>^&1^|find "REG_"') do @set fn=%%b 
+2
Apr 27 '14 at 20:51
source share

Thanks, I just need to use:

 SETLOCAL EnableExtensions 

And put a:

 2^>nul 

The REG QUERY query called in the FOR command. Thank you very much!:)

0
Jan 15 '09 at 1:36
source share
 @echo off setlocal ENABLEEXTENSIONS set KEY_NAME=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\awhost32.exe set VALUE_NAME=Path for /F "usebackq tokens=3" %%A IN (`reg query "%KEY_NAME%" /v "%VALUE_NAME%" 2^>nul ^| find "%VALUE_NAME%"`) do ( echo %%A ) 

How do you handle whitespace in %% A variable? This leads to C: \ Program. The actual path is C: \ Program Files \ Symantec \ pcAnywhere.

0
May 02 '12 at 2:09 p.m.
source share
 echo Off setlocal ENABLEEXTENSIONS set KEY_NAME="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup" set VALUE_NAME=release REG QUERY %KEY_NAME% /S /v %VALUE_NAME% endlocal 

dot put \ at the end of KEY_NAME

0
Jun 27 '13 at 11:41
source share

I encountered many errors on computers running Windows XP when using WMIC (for example, due to corrupted files on machines). Therefore, it is best to use WMIC for Win XP in code. No problem with WMIC on Win 7 though.

0
Jan 19 '15 at 15:13
source share

You can get the value of the registry key as shown below

 @echo OFF setlocal ENABLEEXTENSIONS set REG_NAME="HKEY_CURRENT_USER\Software\Test" set KEY_NAME=TestVal FOR /F "usebackq skip=2 tokens=1-3" %%A IN (`REG QUERY %REG_NAME% /v %KEY_NAME% 2^>nul`) DO ( @echo %%A : %%C ) pause 

those who are wondering how to add reg keys, here is the way.

 REGEDIT4 ; @ECHO OFF ; CLS ; REGEDIT.EXE /S "%~f0" ; EXIT [HKEY_CURRENT_USER\Software\Test] "TestVal"="Succeeded" 
0
Dec 17 '15 at 6:29
source share

To get a specific answer to the registry value, you can use the following query:

REG QUERY "Key_Name" / v "Value_Name" / s

for example: REG QUERY "HKEY_CURRENT_USER \ Software \ Microsoft \ Shell" / v "EnableExtensions" / s

here / v: Queries for specific registry key values.

/ s: query all subkeys and values ​​recursively (e.g. dir / s)

0
Jun 01 '17 at 6:01
source share
 set regVar_LocalPrjPath="LocalPrjPath" set regVar_Path="HKEY_CURRENT_USER\Software\xyz\KeyPath" :: ### Retrieve VAR1 ### FOR /F "skip=2 tokens=2,*" %%A IN ('reg.exe query %regVar_Path% /v %regVar_LocalPrjPath%') DO set "VAR1=%%B" 
-one
Oct 20 '17 at 20:18
source share



All Articles