Jump to content

Recommended Posts

I'm doing a RegEnumKey to look for a string and delete the key if it finds them, and it works.  This is a snippet of the entire script.

    ; Loop from 1 to 50 times
    For $i = 1 To 50
    
        $sSubKey = RegEnumKey("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList", $i)
        If StringInStr($sSubKey, $s_keyval) Then MsgBox (64, "  CORP.FOX  Profile  Cleanup", "Delete   " & $pguid & $guid)
        If StringInStr($sSubKey, $s_keyval) Then _FileWriteLog ( $sLogPath, "Running RegDelete " & " (" & $plist & $sSubKey & ")")
        If StringInStr($sSubKey, $s_keyval) Then RegDelete($plist & $sSubKey)
  
    Next

The problem I'm having is, when the script sees the string I'm searching for, the script deletes the registry key,  BUT it also breaks the loop.  So if there's two keys with the string, it wont find the second regkey unless i re-run the script.

Can anyone provide some assistance on how I can delete the key but not break the loop.

Thanks in advance.

 

Link to post
Share on other sites

You need error checking.

$i = 0
    Do
        $i += 1
        $sSubKey = RegEnumKey("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList", $i)
          If StringInStr($sSubKey, $s_keyval) Then 
              MsgBox (64, "  CORP.FOX  Profile  Cleanup", "Delete   " & $pguid & $guid)
              _FileWriteLog ( $sLogPath, "Running RegDelete " & " (" & $plist & $sSubKey & ")")
              RegDelete($plist & $sSubKey)
              ConsoleWrite($i&@CRLF)
          EndIf
    Until $i = 50

This way you are more flexible for debugging.

I think the problem is the logic. The return value is not an array. May a string split with carriage return. Then you check for each row. 

Because when you check string you don't delete anything from the first return. We don't have all your code but I think it is something like that. 

#include <MsgBoxConstants.au3>
#include <Array.au3>

Example()

Func Example()
    Local $sSubKey = "", $sEnumKey = "" & @CRLF & @CRLF

    ; Loop from 1 to 50 times, recording registry keys into $sSubKey.
    For $i = 1 To 50
        $sSubKey = RegEnumKey("HKEY_LOCAL_MACHINE\SOFTWARE", $i)
        If @error Then ExitLoop
        $sEnumKey &= $sSubKey & @CRLF
    Next

    ; Cheking the results.
    MsgBox($MB_SYSTEMMODAL, "RegEnumKey Example", $sEnumKey)
    ; Spliting the result into an array
    $sEnumKey = StringSplit ( $sEnumKey , @CRLF )
    
    If Not IsArray ( $sEnumKey ) Then
        MsgBox(64,"ERROR","")
        Exit
    Else
        ; Cheking the results.
        _ArrayDisplay ($sEnumKey)

        ; Loop from 1 to $sEnumKey Rows, deleting registry keys.
        For $i = 1 To UBound ($sEnumKey) -1
            MsgBox (64, "  CORP.FOX  Profile  Cleanup", "Delete   " & $pguid & $guid)
            _FileWriteLog ( $sLogPath, "Running RegDelete " & " (" & $plist & $sEnumKey[$i] & ")")
            RegDelete($plist & $sEnumKey[$i])
            ConsoleWrite($i&@CRLF)
        Next
    EndIf
EndFunc   ;==>Example

 

Edited by caramen

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to post
Share on other sites

Because you're deleting an item, you don't want to increment the counter so use ContinueLoop, see example below:

#RequireAdmin
#include <File.au3>
Local $sLogPath = "C:\Temp\RemoveProfile.log", $pguid, $guid, $plist, $s_keyval = "xyz"
Local $sProfileList = (@OSArch = "x64" ? "HKLM64" : "HKLM") & "\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
Local $i = 1, $sSubKey
While 1
    $sSubKey = RegEnumKey($sProfileList, $i)
        If @error Then ExitLoop
    If StringInStr($sSubKey, $s_keyval) Then
        MsgBox (64, "  CORP.FOX  Profile  Cleanup", "Delete   " & $sProfileList & "\" & $sSubKey)
        _FileWriteLog ( $sLogPath, "Running RegDelete " & " (" & $sProfileList & "\" & $sSubKey & ")")
        RegDelete($sProfileList & "\" & $sSubKey)
        ContinueLoop
    EndIf
    $i += 1
WEnd

 

Link to post
Share on other sites
5 hours ago, keilamym said:

I tried @Subz options first because it seemed the easiest for this novice. I also added more error checking @caramen and everything seems to be working. 

The @Subz suggestion was the obvious solution.

 Adding error checking is not going to fix anything unless you analyse the reported errors and then modify the code to cope with the error conditions. I don't think this was your solution.

Phil Seakins

Link to post
Share on other sites
8 hours ago, pseakins said:

I don't think this was your solution.

Error checking isn't solution, it's a must. 

 

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to post
Share on other sites

my script did have some error checking already but most of the script was removed to make the question easier to ask. in my script however i found a condition where if the profile guid subkey didnt have a value, it would delete the entire root key, which would be bad. lol. 

granted, the script performs a backup of the keys first but id rather not have to use it.

again, thanks everyone for your help. 

Link to post
Share on other sites

Yeah, I trust you :). I've already seen you post impressive code.

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...