Loop (registry)

Retrieves the contents of the specified registry subkey, one item at a time.

Loop Reg, KeyName , Mode

Parameters

Reg

The literal word Reg (case-insensitive). This cannot be a variable or expression.

KeyName

The full name of the registry key.

This must start with HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CURRENT_USER, HKEY_CLASSES_ROOT, or HKEY_CURRENT_CONFIG (or the abbreviations for each of these, such as HKLM). To access a remote registry, prepend the computer name and a slash, as in this example: \\workstation01\HKEY_LOCAL_MACHINE

Mode

Zero or more of the following letters:

K: Include keys.
V: Include values. Values are also included if both K and V are omitted.
R: Recurse into subkeys. If R is omitted, keys and values within subkeys of Key are not included.

Remarks

A registry-loop is useful when you want to operate on a collection registry values or subkeys, one at a time. The values and subkeys are retrieved in reverse order (bottom to top) so that RegDelete can be used inside the loop without disrupting the loop.

The following variables exist within any registry-loop. If an inner registry-loop is enclosed by an outer registry-loop, the innermost loop's registry item will take precedence:

A_LoopRegName Name of the currently retrieved item, which can be either a value name or the name of a subkey. Value names displayed by Windows RegEdit as "(Default)" will be retrieved if a value has been assigned to them, but A_LoopRegName will be blank for them.
A_LoopRegType The type of the currently retrieved item, which is one of the following words: KEY (i.e. the currently retrieved item is a subkey not a value), REG_SZ, REG_EXPAND_SZ, REG_MULTI_SZ, REG_DWORD, REG_QWORD, REG_BINARY, REG_LINK, REG_RESOURCE_LIST, REG_FULL_RESOURCE_DESCRIPTOR, REG_RESOURCE_REQUIREMENTS_LIST, REG_DWORD_BIG_ENDIAN (probably rare on most Windows hardware). It will be empty if the currently retrieved item is of an unknown type.
A_LoopRegKey The full name of the key which contains the current loop item. For remote registry access, this value will not include the computer name.
A_LoopRegTimeModified The time the current subkey or any of its values was last modified. Format YYYYMMDDHH24MISS. This variable will be empty if the currently retrieved item is not a subkey (i.e. A_LoopRegType is not the word KEY).

When used inside a registry-loop, the following functions can be used in a simplified way to indicate that the currently retrieved item should be operated upon:

RegRead, OutputVar Reads the current item. If the current item is a key, ErrorLevel will be set to 1 and OutputVar will be made empty.
RegWrite [, Value] Writes to the current item. If Value is omitted, the item will be made 0 or blank depending on its type. If the current item is a key, ErrorLevel will be set to 1 and there will be no other effect.
RegDelete Deletes the current item. If the current item is a key, it will be deleted along with any subkeys and values it contains.

When accessing a remote registry (via the KeyName parameter described above), the following notes apply:

See Loop for information about Blocks, Break, Continue, and the A_Index variable (which exists in every type of loop).

Related

Loop, Break, Continue, Blocks, RegRead, RegWrite, RegDelete, SetRegView

Examples

; Example: Delete Internet Explorer's history of URLs typed by the user:
Loop Reg, "HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\TypedURLs"
    RegDelete

 

; Example: A working test script:
Loop Reg, "HKCU\Software\Microsoft\Windows", "R KV"  ; Recursively retrieve keys and values.
{
    if a_LoopRegType = "key"
        value := ""
    else
    {
        value := RegRead()
        if ErrorLevel
            value := "*error*"
    }
    Result := MsgBox(a_LoopRegName " = " value " (" a_LoopRegType ")`n`nContinue?",, "y/n")
}
Until Result = "No"

 

; Example: A working example to recursively search the entire
; registry for particular value(s).
RegSearch("Notepad")
return

RegSearch(Target)
{
    ContinueRegSearch := true
    Loop Reg, "HKEY_LOCAL_MACHINE", "KVR"
    {
        Gosub CheckThisRegItem
        if !ContinueRegSearch  ; It told us to stop.
            return
    }
    Loop Reg, "HKEY_USERS", "KVR"
    {
        Gosub CheckThisRegItem
        if !ContinueRegSearch  ; It told us to stop.
            return
    }
    Loop Reg, "HKEY_CURRENT_CONFIG", "KVR"
    {
        Gosub CheckThisRegItem
        if !ContinueRegSearch  ; It told us to stop.
            return
    }
    ; Note: I believe HKEY_CURRENT_USER does not need to be searched if HKEY_USERS is
    ; being searched.  Similarly, HKEY_CLASSES_ROOT provides a combined view of keys from
    ; HKEY_LOCAL_MACHINE and HKEY_CURRENT_USER, so searching all three isn't necessary.
    return

CheckThisRegItem:
    if A_LoopRegType = "KEY"  ; Remove these two lines if you want to check key names too.
        return
    RegValue := RegRead()
    if ErrorLevel
        return
    if InStr(RegValue, Target)
    {
        Result := MsgBox(Format("
        (
        The following match was found:
        {1}\{2}
        Value = {3}
        
        Continue?
        )", A_LoopRegKey, A_LoopRegName, RegValue),, "y/n")
        if Result = "No"
            ContinueRegSearch := false  ; Tell our caller to stop searching.
    }
    return
}