Check Registry value if Exist then next instruction

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Check Registry value if Exist then next instruction

#1 Post by booga73 » 30 Aug 2013 19:56

I have a batch script that I am trying to write.
This script searches for a registry key that needs to verify a value name which the value name identifies with a value data.

Such as the following registry sample:

Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]
"value1"="0"

registry key: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings
value name: value1
value data: 0


My code that I am using:

SETLOCAL DisableDelayedExpansion

FOR /F "usebackq tokens=3*" %%A IN (`REG QUERY "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v value1`) DO (
set appdir=%%A %%B
)
If errorlevel 0 If %appdir%=="0" (
goto Label2
) Else (
goto label3

endlocal

As a registry test, I enter value1 with a value of 0 into my registry of that registry key. When I run my script I would like the script to continue with additional code if %appdir% does equal 0, but if %appdir" doesn't equal 0 then proceed onto a different goto statement.

Is there a way to check the correct value in %appdir% with the required value of 0?

v/r booga73

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Check Registry value if Exist then next instruction

#2 Post by foxidrive » 31 Aug 2013 01:47

You have to also cater for results like this - where the value name has spaces.

Code: Select all

REG QUERY "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v "user agent"

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings
    user agent    REG_SZ    Mozilla/4.0 (compatible; MSIE 8.0; Win32)





It helps to know what the real task is - or you will get code that fails on situations like the above.

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Check Registry value if Exist then next instruction

#3 Post by booga73 » 31 Aug 2013 05:35

Hello,

I am trying to verify if a registry key does exist, and verify then if the value name does exist then verify if value data is a specific value. Does that help?

for instance, I managed to get this script works till it exits from the for loop to process the errorlevel / if statement to decide if %%C is identical to Value_Name2.

I am running Windows7.

@echo off
setlocal ENABLEEXTENSIONS
set KEY_NAME="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer"
set VALUE_NAME1="Version"
set VALUE_NAME2="8.0.7601.17514"

FOR /F "usebackq skip=2 tokens=1,2*" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (
set ValueName=%%A
set ValueType=%%B
set Home=%%C
echo %%A >> c:\temp1\test.txt
echo %%B >> c:\temp1\test.txt
echo %%C >> c:\temp1\test.txt
)

IF Errorlevel 0 IF %VALUE_NAME2%==%%C (
goto task1
) else (
task2
)

:task1
echo The Value Name #2 doesn't equal String.

:task2
echo The Value Name #2 does equal string.

pause

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Check Registry value if Exist then next instruction

#4 Post by booga73 » 31 Aug 2013 06:03

I saw an issue with the prior batch script and would like to repost as this update works; I've tested it.


@echo off
set KEY_NAME="HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
set VALUE_NAME="CoInternetCombineIUriCacheSize"

FOR /F "usebackq skip=2 tokens=1-3" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (
set ValueName=%%A
set ValueType=%%B
set Home=%%C
)

echo %Home%

pause



In this case the output = 0x50, on my machine, Win7 32bit.

now, considering that is the true output of what my machine has listed as the value data in the registry, I would like to do an IF than else statement, such as the pseudo code:

IF %Home%==0x50 (
goto task1
) else (
task2
)

Hope that helps better to understand.

v/r booga73

Post Reply