Friday, 26 September 2014

Find value of registry key in a bat file

@echo off

set THEME_REGKEY=HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Outlook\AutoDiscover
set THEME_REGVAL=ameritech.net

REM Check for presence of key first.
reg query %THEME_REGKEY% /v %THEME_REGVAL% 2>nul || (echo No theme name present! & exit /b 1)

REM query the value. pipe it through findstr in order to find the matching line that has the value. only grab token 3 and the remainder of the line. %%b is what we are interested in here.
set REG_NAME=
for /f "tokens=2,*" %%a in ('reg query %THEME_REGKEY% /v %THEME_REGVAL% ^| findstr %THEME_REGVAL%') do (
    set REG_NAME=%%b
)

REM Possibly no value set
if not defined REG_NAME (echo No theme name present! & exit /b 1)

REM replace any spaces with +
set REG_NAME=%REG_NAME: =+%

REM open up the default browser, searching google for the theme name
echo %REG_NAME%

No comments:

Post a Comment