Registry find & replace help

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ksegrist
Posts: 2
Joined: 02 Jan 2008 06:23

Registry find & replace help

#1 Post by ksegrist » 02 Jan 2008 06:31

Hi,

I'm trying to find a way to search the registry for keys having value "filename=urlmon.dll" and check the value "Location" to ensure the proper (requested) path. If the path isn't what it should be, to replace it with the proper one... and to do this during an unattended installation...

I was hoping a batch file calling regfind.exe will help, but not certain whether regfind supports "find next".

Does anyone have ideas? (regfind is not a requirement, but unatended is... no gui allowed)

Thanks in advance for any suggestions... and Happy New Year!

kev

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#2 Post by DosItHelp » 04 Jan 2008 00:48

ksegrist,

Copy the following function at the end of your batch file:

Code: Select all

:CkRegValueFilePath RegPath RegKey FilePath -- corrects a file path referenced by a registry value if needed
::      -- RegPath  [in] - registry path to be parsed recursively for the RegKey
::      -- RegKey   [in] - registry key whose value is expected to be a file name in the given FilePath
::      -- FilePath [in] - file path to be check and corrected if needed
:$source http://www.dostips.com
SETLOCAL
set "QueryPath=%~1"
set "QueryKey=%~2"
set "ExpectedFilePath=%~3"

::: make sure File Path end with a back slash
if "%ExpectedFilePath:~-1%" NEQ "\" set "ExpectedFilePath=%ExpectedFilePath%\"

::: export all sub keys and values for analysis
reg export "%QueryPath%" r0.txt>NUL || goto:EOF

set "RegPath="
::: convert t0.txt from UNICODE to ASCII, filter RegPath and Key-Value in question and loop thru them
for /f "tokens=*" %%A in ('" cmd /a /c type r0.txt | findstr /b /c:"[" /c:"\"%QueryKey%\"" "') do (
    ::: split the line at the '=' operator
    for /f "tokens=1,* delims==" %%B in ("%%A") do (
        if "%%C"=="" (
            ::: no '=' operator was found, assume current line is a Reg-Path
            set "RegPath=%%B"
            call set "RegPath=%%regpath:~1,-1%%"  &rem strip brackets off the Reg-Path
        ) else (
            ::: '=' operator was found, this line is a value assignment, let's check the referenced path
            if "%%~dpC" NEQ "%ExpectedFilePath%" (
                ::: file path needs correction, but leave file name unchanged
                call ECHO reg add "%%RegPath%%" /v "%%~B" "%%ExpectedFilePath%%%%~nxC"
            )
        )
    )
)
GOTO:EOF


In your main code call the function like this:
call:CkRegValueFilePath "HKLM\SOFTWARE\YOURREGISTRYPATH" "filename" "C:\Program Files\MyExpectedPath"
...
GOTO:EOF


Two things to notice.
1. The "reg add" command that actually corrects the registry value is currently not executed but displayed on the screen, after thorough testing simply remove the "ECHO" command from the line.
2. A temporary registry export file named r0.txt is created in the "CURRENT" directory. There might be a way to avoid this if needed.

Let me know what you think.
DosItHelp? :wink:

ksegrist
Posts: 2
Joined: 02 Jan 2008 06:23

#3 Post by ksegrist » 04 Jan 2008 06:45

Hi,

Thanks for the post... I didn't have much luck testing your suggestion, (I'm sure that was my own fault, I probably didn't implement it as you intended), but wound up using AutoIT as follows:

Code: Select all

for $i = 1 to 200
   $kb = RegEnumKey("HKLM\Software\Microsoft\Updates\Windows XP\SP3\", $i)
   for $j = 1 to 99
      $key = regenumkey("HKLM\Software\Microsoft\Updates\Windows XP\SP3\" & $kb & "\filelist\", $j)
      for $k = 1 to 99
         $val = regenumval("HKLM\Software\Microsoft\Updates\Windows XP\SP3\" & $kb & "\filelist\" & $key, $k)
         if $val = "FileName" then
            $dval = RegRead("HKLM\Software\Microsoft\Updates\Windows XP\SP3\" & $kb & "\filelist\" & $key, "FileName")
            $dloc = RegRead("HKLM\Software\Microsoft\Updates\Windows XP\SP3\" & $kb & "\filelist\" & $key, "Location")
            if $dval = "urlmon.dll" AND $dloc <> "c:\windows\system32" Then
               RegWrite("HKLM\Software\Microsoft\Updates\Windows XP\SP3\" & $kb & "\filelist\" & $key, "Location", "REG_SZ", "C:\windows\system32")
            endif
         EndIf
      Next
   next
next

Thanks again for your assistance!

ksegrist

Post Reply