Sign in to follow this  
Followers 0
TuMiM

Registry search and delete

10 posts in this topic

I am trying to search the registry for with the ODBC.INI key for entries with matching "database" and "server" names and then delete those keys. The code is not efficient but the registry search functions by holger work at least. Problem is, when there are more than 1 entry it obviously doesn't work. What is a better way to do this?

#include <GUIConstants.au3>

Global $found = ""
Global $found1 = ""

SearchReg("HKLM\SOFTWARE\ODBC\ODBC.INI","winmls")
SearchReg1("HKLM\SOFTWARE\ODBC\ODBC.INI","miasql2")

if $found = $found1 Then
Msgbox(0,"",$found)
RegDelete($found)
EndIF
Exit


;*****************************************************
; Recursive search-function
;*****************************************************

Func SearchReg($startkey,$searchval)
    Local $startkey,$val,$i,$key,$searchval,$z
    
    $i = 1
    While 1
        $key = RegEnumKey($startkey,$i)
        If @error <> 0 Then ExitLoop
        $z = 1
        While 1
            $val = RegEnumVal($startkey & "\" & $key,$z)
            If @error <> 0 Then ExitLoop
            $readval = RegRead($startkey & "\" & $key,$val)
            If $readval <> "" And StringInStr($readval,$searchval) Then $found = $found & $startkey & "\" & $key
            $z = $z + 1
        WEnd
        SearchReg($startkey & "\" & $key,$searchval)
        $i = $i + 1
    WEnd

EndFunc

Func SearchReg1($startkey1,$searchval1)
    Local $startkey1,$val1,$i1,$key1,$searchval1,$z1
    
    $i1 = 1
    While 1
        $key1 = RegEnumKey($startkey1,$i1)
        If @error <> 0 Then ExitLoop
        $z1 = 1
        While 1
            $val1 = RegEnumVal($startkey1 & "\" & $key1,$z1)
            If @error <> 0 Then ExitLoop
            $readval1 = RegRead($startkey1 & "\" & $key1,$val1)
            If $readval1 <> "" And StringInStr($readval1,$searchval1) Then $found1 = $found1 & $startkey1 & "\" & $key1
            $z1 = $z1 + 1
        WEnd
        SearchReg1($startkey1 & "\" & $key1,$searchval1)
        $i1 = $i1 + 1
    WEnd

EndFunc

Share this post


Link to post
Share on other sites



can anyone help?

Share this post


Link to post
Share on other sites

$database = "winmls"
$server = "miasql2"
$startkey = "HKLM\SOFTWARE\ODBC\ODBC.INI"
$i = 1
While 1
   $key = RegEnumKey($startkey, $i)
   If @error <> 0 Then ExitLoop
   If RegRead($startkey & "\" & $key,"Database") = $database And RegRead($startkey & "\" & $key,"Server") = $server Then RegDelete($startkey & "\" & $key)
   $i = $i + 1
WEnd

Share this post


Link to post
Share on other sites

Thanks but that only deletes one entry even if there are multiple

Share this post


Link to post
Share on other sites

Thanks but that only deletes one entry even if there are multiple

this will delete all keys that matches.

Share this post


Link to post
Share on other sites

this will delete all keys that matches.

It should butit does not. I have tested it and it only deletes one entry for every time i run it even though there are multiple entries that match the criteria

Share this post


Link to post
Share on other sites

It should butit does not. I have tested it and it only deletes one entry for every time i run it even though there are multiple entries that match the criteria

then my guess is that you have casing problem try this.

$database = "winmls"
$server = "miasql2"
$startkey = "HKLM\SOFTWARE\ODBC\ODBC.INI"
$i = 1
While 1
   $key = RegEnumKey($startkey, $i)
   If @error <> 0 Then ExitLoop
   If StringLower(RegRead($startkey & "\" & $key,"Database")) = $database And StringLower(RegRead($startkey & "\" & $key,"Server")) = $server Then RegDelete($startkey & "\" & $key)
   $i = $i + 1
WEnd

Share this post


Link to post
Share on other sites

then my guess is that you have casing problem try this.

$database = "winmls"
$server = "miasql2"
$startkey = "HKLM\SOFTWARE\ODBC\ODBC.INI"
$i = 1
While 1
   $key = RegEnumKey($startkey, $i)
   If @error <> 0 Then ExitLoop
   If StringLower(RegRead($startkey & "\" & $key,"Database")) = $database And StringLower(RegRead($startkey & "\" & $key,"Server")) = $server Then RegDelete($startkey & "\" & $key)
   $i = $i + 1
WEnd
No, it is not a casing problem. Even with this code or even if i change database value to upper case, it will still only delete one entry that matches each time the Au3 is run not all at one time.

Share this post


Link to post
Share on other sites

change if statements.

If StringLower(RegRead($startkey & "\" & $key,"Database")) = $database And StringLower(RegRead($startkey & "\" & $key,"Server")) = $server Then 
       RegDelete($startkey & "\" & $key)
       $i = $i -1
   EndIf

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

$database = "winmls"
$server = "miasql2"
$startkey = "HKLM\SOFTWARE\ODBC\ODBC.INI"
$i = 1
While 1
   $key = RegEnumKey($startkey, $i)
   If @error <> 0 Then ExitLoop
   If RegRead($startkey & "\" & $key,"Database") = $database And RegRead($startkey & "\" & $key,"Server") = $server Then RegDelete($startkey & "\" & $key)
   $i = $i + 1
WEnd
I think there is a logic error in this code. If you delete enumerated key '1' then the old enumerated key '2' BECOMES '1'. That causes your increment of $i to skip over it. The following fixes that problem by using ContinueLoop to avoid incrementing $i:

$database = "winmls"
$server = "miasql2"
$startkey = "HKLM\SOFTWARE\ODBC\ODBC.INI"
$i = 1
While 1
   $key = RegEnumKey($startkey, $i)
   If @error <> 0 Then ExitLoop
   If RegRead($startkey & "\" & $key,"Database") = $database And RegRead($startkey & "\" & $key,"Server") = $server Then 
      RegDelete($startkey & "\" & $key)
      ContinueLoop
   EndIf
   $i = $i + 1
WEnd

Hope that helps! :-D

P.S. Just noticed Joon's post #9 had covered that already... sorry for the redundant redundancy! :">

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0