Jump to content
Sign in to follow this  
JoymanyMahn

Looping Through Variables/Simplifying Code

Recommended Posts

JoymanyMahn

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.

Share this post


Link to post
Share on other sites
PsaltyDS

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

Share this post


Link to post
Share on other sites
Zedna

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

Share this post


Link to post
Share on other sites
JoymanyMahn

Arrays are indeed our friends! And now, thanks to your great example, Im able to take advantage of that new great friendship. Thanks again! I feel like I am actually starting to get some of this stuff :-)

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  

×