Jump to content

Looping Through Variables/Simplifying Code


Recommended Posts

Could someone tell (inexperienced) me how to loop through a set of variables? For example: Lets say I want to delete five Registry values. Rather than repeating the logic five times, could it just loop through $RV1 - $RV5 so the code just has to be written once?

$RV1 = ("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32")
$RV2 = ("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\RecentDocs")
$RV3 = ("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU")
$RV4 = ("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\StreamMRU")
$RV5 = ("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\UserAssist")

RegRead($RV1,"")

If @error Then
    $RV1_Results = ("Error: Key might not be present: " &  $RV1)

    Else
    RegDelete ($RV1)
            
        If @error Then
        $RV1_Results = ("Problem with deleting key: " &  $RV1)
    
            Else
            $RV1_Results = ("Successfully deleted value: " & $RV1)
                
        EndIf
EndIf

MsgBox(4096, "Results", $RV1_Results,)

Any help you could offer this newbie would be appreciated.

Thank you.

Link to comment
Share on other sites

Like this:

$RV = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\"
Dim $avRegKeys[5] = ["ComDlg32", "RecentDocs", "RunMRU", "StreamMRU", "UserAssist")
$RV_Results = ""

For $n = 0 To 4
    RegRead($RV, $avRegKeys[$n])
    If @error Then
        $RV_Results &= "Error: Key might not be present: " & $RV1 & $avRegKeys[$n] & @CRLF
    Else
        RegDelete($RV & $avRegKeys[$n])
        If @error Then
            $RV_Results &= "Problem with deleting key: " & $RV & $avRegKeys[$n] & @CRLF
        Else
            $RV_Results &= "Successfully deleted value: " & $RV & $avRegKeys[$n] & @CRLF
        EndIf
    EndIf
Next
MsgBox(4096, "Results", $RV_Results)

Repeat after me: Arrays are our friends, arrays are our friends, arrays are our friends...

:)

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
Link to comment
Share on other sites

Action("ComDlg32")
Action("RecentDocs")
Action("RunMRU")
Action("StreamMRU")
Action("UserAssist")

Func Action($what)
    $RV1 = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\" & $what
    RegRead($RV1,"")

    If @error Then
        $RV1_Results = ("Error: Key might not be present: " &  $RV1)
    Else
        RegDelete ($RV1)
        If @error Then
            $RV1_Results = ("Problem with deleting key: " &  $RV1)
        Else
            $RV1_Results = ("Successfully deleted value: " & $RV1)
        EndIf
    EndIf

    MsgBox(4096, "Results", $RV1_Results)
EndFunc

EDIT: Functions are our friends too :)

Edited by Zedna
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...