Loop (registry)

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

New Syntax [v1.1.21+]

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 such as HKLM\Software or %FullPathOfKey%.

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 colon, as in this example: \\workstation01:HKEY_LOCAL_MACHINE

Mode

If blank or omitted, only values are included and subkeys are not recursed into. Otherwise, specify one or more of the following letters:

Old Syntax

Deprecated: This syntax is not recommended for use in new scripts. Use the new syntax instead.

Loop, RootKey , Key, IncludeSubkeys?, Recurse?

Parameters

RootKey

Must be either HKEY_LOCAL_MACHINE (or HKLM), HKEY_USERS (or HKU), HKEY_CURRENT_USER (or HKCU), HKEY_CLASSES_ROOT (or HKCR), or HKEY_CURRENT_CONFIG (or HKCC).

To access a remote registry, prepend the computer name and a colon, as in this example: \\workstation01:HKEY_LOCAL_MACHINE

Key

The name of the key (e.g. Software\SomeApplication). If blank or omitted, the contents of RootKey will be retrieved.

IncludeSubkeys?

If blank or omitted, it defaults to 0 (only values are retrieved). Otherwise, specify one of the following digits:

Recurse?

If blank or omitted, it defaults to 0 (subkeys are not recursed into). Otherwise, specify one of the following digits:

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:

Variable Description
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 name of the root key being accessed (HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CURRENT_USER, HKEY_CLASSES_ROOT, or HKEY_CURRENT_CONFIG). For remote registry access, this value will not include the computer name.
A_LoopRegSubKey Name of the current subkey. This will be the same as the Key parameter unless the Recurse parameter is being used to recursively explore other subkeys. In that case, it will be the full path of the currently retrieved item, not including the root key. For example: Software\SomeApplication\My SubKey
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 commands can be used in a simplified way to indicate that the currently retrieved item should be operated upon:

Syntax Description
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
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, 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 RootKey or 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).

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

Examples

Deletes Internet Explorer's history of URLs typed by the user.

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

A working test script.

Loop, Reg, HKEY_CURRENT_USER\Software\Microsoft\Windows, KVR
{
    if (A_LoopRegType = "key")
        value := ""
    else
    {
        RegRead, value
        if ErrorLevel
            value := "*error*"
    }
    MsgBox, 4, , %A_LoopRegName% = %value% (%A_LoopRegType%)`n`nContinue?
    IfMsgBox, NO, break
}

Recursively searches the entire registry for particular value(s).

SetBatchLines -1  ; Makes searching occur at maximum speed.
RegSearchTarget := "Notepad"  ; Tell the subroutine what to search for.
Gosub, RegSearch
return

RegSearch:
ContinueRegSearch := true
Loop, Reg, HKEY_LOCAL_MACHINE, KVR
{
    Gosub, CheckThisRegItem
    if not ContinueRegSearch ; It told us to stop.
        return
}
Loop, Reg, HKEY_USERS, KVR
{
    Gosub, CheckThisRegItem
    if not ContinueRegSearch ; It told us to stop.
        return
}
Loop, Reg, HKEY_CURRENT_CONFIG, KVR
{
    Gosub, CheckThisRegItem
    if not 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.  The same might also be true for HKEY_CLASSES_ROOT if
; HKEY_LOCAL_MACHINE is being searched.
return

CheckThisRegItem:
if (A_LoopRegType = "KEY")  ; Remove these two lines if you want to check key names too.
    return
RegRead, RegValue
if ErrorLevel
    return
if InStr(RegValue, RegSearchTarget)
{
    MsgBox, 4, , The following match was found:`n%A_LoopRegKey%\%A_LoopRegSubKey%\%A_LoopRegName%`nValue = %RegValue%`n`nContinue?
    IfMsgBox, No
        ContinueRegSearch := false  ; Tell our caller to stop searching.
}
return