Loop Reg

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

Loop Reg KeyName , Mode

Parameters

KeyName

Type: String

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

Type: String

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 and RegDeleteKey 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:

Value := RegRead() Reads the current item. If the current item is a key, an exception is thrown.
RegWrite Value
RegWrite
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, an exception is thrown and the registry is not modified.
RegDelete Deletes the current item if it is a value. If the current item is a key, its default value will be deleted instead.
RegDeleteKey Deletes the current item if it is a key. If the current item is a value, the key which contains that value will be deleted, including all subkeys and values.

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, RegDeleteKey, SetRegView

Examples

#1: Delete Internet Explorer's history of URLs typed by the user:

Loop Reg, "HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\TypedURLs"
    RegDelete

#2: A working test script:

Loop Reg, "HKCU\Software\Microsoft\Windows", "R KV"  ; Recursively retrieve keys and values.
{
    if A_LoopRegType = "key"
        value := ""
    else
    {
        try
            value := RegRead()
        catch
            value := "*error*"
    }
    Result := MsgBox(A_LoopRegName " = " value " (" A_LoopRegType ")`n`nContinue?",, "y/n")
}
Until Result = "No"

#3: A working example to recursively search the entire registry for particular value(s).

RegSearch("Notepad")

RegSearch(Target)
{
    Loop Reg, "HKEY_LOCAL_MACHINE", "KVR"
    {
        if !CheckThisRegItem()  ; It told us to stop.
            return
    }
    Loop Reg, "HKEY_USERS", "KVR"
    {
        if !CheckThisRegItem()  ; It told us to stop.
            return
    }
    Loop Reg, "HKEY_CURRENT_CONFIG", "KVR"
    {
        if !CheckThisRegItem()  ; 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.

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