Sign in to follow this  
Followers 0
lolipop

Problem with For loop in deleting registry key

19 posts in this topic

Hi members,

I'm trying to use a FOR loop to delete some registry key base on certain condition but somehow it's not working correctly.

Basically, inside registry key test1 there's a couple of other keys nested. What I want is to delete all the other keys and leave the keys I want intact. The code below seems to read and delete every alternate keys instead. TIA.

Below is an example of the code.

For $count = 1 To 1000
$var = RegEnumKey("HKEY_CURRENT_USER\test\test1\", $count)
If @error <> 0 Then ExitLoop
If $var <> "S-1-1-1-1" Or "S-1-1-1-2" Or "S-1-1-1-3" Then
$var1 = ("HKEY_CURRENT_USER\test\test1\" & $var)
RegDelete($var1)
EndIf
Next

Share this post


Link to post
Share on other sites



This line is wrong.

If $var <> "S-1-1-1-1" Or "S-1-1-1-2" Or "S-1-1-1-3" Then

It needs to be written like this.

If $var <> "S-1-1-1-1" Or $var <> "S-1-1-1-2" Or $var <> "S-1-1-1-3" Then

You have to test the comparison against something every time, you can't specify it once like you were doing.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Hi BrewMan,

I have tried that as well but the code doesn't execute correctly as well. It doesn't seems to be checking the condition properly. I try changing the code as per your advise, now it delete the registry key "S-1-1-1-2" as well. The code seems to be deleting in the alternate pattern.

Read key 1

Read key 2

Delete key 2

Read key 3

Read key 4

Delete key 4

Read key 5

Read key 6

Delete key 6

etc......

Share this post


Link to post
Share on other sites

Hi guys,

Thanks. I have resolved the problem. Please close the thread. Thank you. :)

Share this post


Link to post
Share on other sites

Hi guys,

Thanks. I have resolved the problem. Please close the thread. Thank you. :)

Just for your information:

Threads such as this are not usually closed on demand, remember, you might have started the thread but it does not belong to you.

Also, it customary, to post solutions to problems you have asked for help with, even if you solve it yourself, as other people read this too.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Well, I'm not gd with explaination. Basically the for loop is giving the wrong registry key instance everytime I delete 1 of the key so I nested the for loop within a while loop and everytime I delete 1 registry key, I restart the for loop process again. Perhaps the amended code below can self explain better. :)

$counter = 1
While $counter
   For $count = 1 To 100
   $list = RegEnumKey("HKEY_CURRENT_USER\TEST\TEST1\List\", $count)
   If @error <> 0 Then
   $counter = 0
   ExitLoop
   EndIf
   If $list = "S-1-5-18" Then
   ElseIf $list = "S-1-5-19" Then
   ElseIf $list = "S-1-5-20" Then
   Else
   RegDelete("HKEY_CURRENT_USER\TEST\TEST1\List\" & $list )
   ExitLoop
   EndIf
   Next
WEnd

There's one thing I still can't figure out and that's the OR operators. Can't seems to get that to work properly. So instead I use If and elseif. Code not optimized but it get the job done.

Share this post


Link to post
Share on other sites

You should probably loop through the registry keys in reverse if you're deleting them. As soon as you delete one, the count is wrong.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

You should probably loop through the registry keys in reverse if you're deleting them. As soon as you delete one, the count is wrong.

How do you loop in reverse?

Share this post


Link to post
Share on other sites

Here's one way, using two loops, the first one gets the count of the keys, the second does the deleting.

; this loop finds out how many subkeys there are
For $i = 1 To 1000
    $var = RegEnumKey("HKEY_CURRENT_USER\test\test1\", $i)
    If @error <> 0 Then
        $Count = $i - 1
        ExitLoop
    EndIf
Next
; This loops through the keys in reverse
For $i = $Count To 1 Step -1
    If $var <> "S-1-1-1-1" Or $var <> "S-1-1-1-2" Or $var <> "S-1-1-1-3" Then
        $var1 = ("HKEY_CURRENT_USER\test\test1\" & $var)
        ConsoleWrite($var1 & @crlf)
    EndIf
Next

I changed the RegDelete to a consolewrite so you don't accidently delete anything before getting it to work right.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Hi Plz Help For Delete Error

dele--1.au3(6,120) : ERROR: RegDelete() [built-in] called with wrong number of args.

RegDelete('HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run','test.exe','REG_SZ',@WindowsDir & "\test.exe")
RegDelete('HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers','C:\Windows\test.exe','REG_SZ','~ RUNASADMIN')

Share this post


Link to post
Share on other sites

AHRIMANSEFID,

And how many arguments does the Help file tell you that RegDelete should have? :huh:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

I read the document you had mentioned but still I have the same problem. Just try the code below:

RegDelete('HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run','test.exe','REG_SZ',@WindowsDir & "\test.exe")
RegDelete('HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers','C:\Windows\test.exe','REG_SZ','~ RUNASADMIN')

when I use and trying these codes I am getting this error:

dele--1.au3(6,120) : ERROR: RegDelete() [built-in]

now can you helping me without getting angry! I appreciate any help suggestion or solution.

OR

RegDelete('HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run','test.exe','REG_SZ','C:\Windows\test.exe')
RegDelete('HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers','C:\Windows\test.exe','REG_SZ','~ RUNASADMIN')

RegDelete('HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run','test.exe','REG_SZ','C:\Windows\test.exe')
Edited by AHRIMANSEFID

Share this post


Link to post
Share on other sites

AHRIMANSEFID,

can you helping me without getting angry!

There was no anger expressed in my previous post - so why suggest that there was? :wacko:

I read the document you had mentioned

And so what is the answer to the question I asked? How many arguments does RegDelete take? :huh:

Once you have that answer then the solution to your problem becomes obvious. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Ok.

You Fix One Line Me.

Share this post


Link to post
Share on other sites

AHRIMANSEFID,

No - you answer the question I asked and then you can then fix all the lines yourself. :)

So, for the third time, how many arguments does the Help file say that you need for RegDelete? :huh:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#RequireAdmin
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Version=Beta
#AutoIt3Wrapper_Compile_Both=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
RegDelete('HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run','Test.exe','REG_SZ','C:\Windows\Test.exe')
RegDelete('HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers','C:\Windows\Test.exe','REG_SZ','~ RUNASADMIN')

RegDelete('HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run','Test.exe','REG_SZ','C:\Windows\Test.exe')
FileDelete(@WindowsDir & "\Test.exe")

Share this post


Link to post
Share on other sites

You Help Me For Fix Line Error.

Thanks A lot.

Me Read Page And Help Autoit.

Not Found Help.

You Help Me.

Share this post


Link to post
Share on other sites

AHRIMANSEFID,

Not Found Help

And to what does the link I posted lead you if not the Help file page for RegDelete? :huh:

I am out of here - even my patience has been exhausted. :bye:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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