Reputation: 401
I am new to batch scripting.I was trying to write a batch file to iterate through all the string values kept at the registry location HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders. And find the result of certain condition. Where the condition is like : a String value with 'Name' field ending with 'Office14'".And I also want to store the 'Name' field of the registry value where the name is ending with 'Office14'.Here is the code which I was using.
@echo OFF
set KEY_NAME="HKLM\Software\Microsoft\Windows\CurrentVersion\Installer\Folders"
set OFFICE=OFFICE14\
set RESULT="NOT FOUND"
FOR %%A IN ('REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders"') DO (
set ValueName=%%A
REM Getting 9 characters from right i.e OFFICE14\ and comparing it to %OFFICE%
set ValueName=%ValueName:~-9%
IF %ValueName%==%OFFICE% (
set RESULT="FOUND"
goto :NEXT
)
)
: NEXT
echo %RESULT%
But the result here is always 'Not Found'. Can anybody help me to fix this code or point me to some helpful documentation for doing the same.
Thanks.
EDIT: Complete value of name (which ends with Office14) is to be stored in a variable.
Upvotes: 1
Views: 5013
Reputation: 37569
reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Installer\Folders"|find /i "\Office14\" >nul 2>&1 && set "RESULT=FOUND"
echo %RESULT%
appended after the question is answered:
target string is:
C:\Program Files\Microsoft Office\Office14\ REG_SZ
getting the path name here is a bit difficult because of trailing spaces/tabs. I suggest a solution with sed for Windows:
for /f "delims=" %%a in ('reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Installer\Folders"^|sed -nr "/\\Office14\\/Is/\s+(.*)\s+REG_SZ/\1/p"') do SET "OFFICEPATH=%%~a"
echo %OFFICEPATH%
And a more advanced solution without sed
:
@ECHO OFF &SETLOCAL
for /f "tokens=*" %%a in ('reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Installer\Folders"^|find /i "\Office14\"') do SET "OFFICEPATH=%%a"
SET "right=REG_SZ%OFFICEPATH:*REG_SZ=%"
CALL SET "OFFICEPATH=%%OFFICEPATH:%right%=%%"
:loop
SET "OFFICEPATH=%OFFICEPATH:~0,-1%"
IF "%OFFICEPATH:~-1%"==" " GOTO :loop
ECHO "%OFFICEPATH%"
Upvotes: 1