keilamym Posted October 27, 2020 Share Posted October 27, 2020 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 comment Share on other sites More sharing options...
caramen Posted October 27, 2020 Share Posted October 27, 2020 (edited) 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 October 27, 2020 by caramen keilamym 1 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 - WikiOutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsPowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & SupportExcel - Example Scripts - WikiWord - Wiki Tutorials:ADO - Wiki Link to comment Share on other sites More sharing options...
Subz Posted October 27, 2020 Share Posted October 27, 2020 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 keilamym 1 Link to comment Share on other sites More sharing options...
keilamym Posted October 27, 2020 Author Share Posted October 27, 2020 (edited) Thank you both. Your help is REALLY appreciated. it's almost 4am here so I'll do some test later today "as time permits" and report back Edited October 27, 2020 by keilamym Link to comment Share on other sites More sharing options...
keilamym Posted October 27, 2020 Author Share Posted October 27, 2020 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. Thank you. both of you are awesome. Link to comment Share on other sites More sharing options...
pseakins Posted October 27, 2020 Share Posted October 27, 2020 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 comment Share on other sites More sharing options...
caramen Posted October 28, 2020 Share Posted October 28, 2020 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 - WikiOutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsPowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & SupportExcel - Example Scripts - WikiWord - Wiki Tutorials:ADO - Wiki Link to comment Share on other sites More sharing options...
keilamym Posted October 28, 2020 Author Share Posted October 28, 2020 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 comment Share on other sites More sharing options...
caramen Posted October 28, 2020 Share Posted October 28, 2020 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 - WikiOutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsPowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & SupportExcel - Example Scripts - WikiWord - Wiki Tutorials:ADO - Wiki Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now