Loop (registry) - Syntax & Usage | AutoHotkey

AutoHotkey

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

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 KeyName are not included.

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?

0 (default) Subkeys contained within Key are not retrieved (only the values).
1 All values and subkeys are retrieved.
2 Only the subkeys are retrieved (not the values).

Recurse?
0 (default) Subkeys are not recursed into.
1 Subkeys are recursed into, so that all values and subkeys contained therein are retrieved.

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

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 RootKey or KeyName parameter described above), the following notes apply:

  • The target machine must be running the Remote Registry service.
  • Access to a remote registry may fail if the target computer is not in the same domain as yours or the local or remote username lacks sufficient permissions (however, see below for possible workarounds).
  • Depending on your username's domain, workgroup, and/or permissions, you may have to connect to a shared device, such as by mapping a drive, prior to attempting remote registry access. Making such a connection -- using a remote username and password that has permission to access or edit the registry -- may as a side-effect enable remote registry access.
  • If you're already connected to the target computer as a different user (for example, a mapped drive via user Guest), you may have to terminate that connection to allow the remote registry feature to reconnect and re-authenticate you as your own currently logged-on username.
  • For Windows Server 2003 and Windows XP Professional, MSDN states: "If the [registry server] computer is joined to a workgroup and the Force network logons using local accounts to authenticate as Guest policy is enabled, the function fails. Note that this policy is enabled by default if the computer is joined to a workgroup."
  • For Windows XP Home Edition, MSDN states that "this function always fails". Home Edition lacks both the registry server and client, though the client can be extracted from one of the OS cab files.

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, HKEY_CURRENT_USER, Software\Microsoft\Internet Explorer\TypedURLs
    RegDelete
; Example: 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
}
; Example: A working example to recursively search 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 = y
Loop, Reg, HKEY_LOCAL_MACHINE, KVR
{
    Gosub, CheckThisRegItem
    if ContinueRegSearch = n ; It told us to stop.
        return
}
Loop, Reg, HKEY_USERS, KVR
{
    Gosub, CheckThisRegItem
    if ContinueRegSearch = n ; It told us to stop.
        return
}
Loop, Reg, HKEY_CURRENT_CONFIG, KVR
{
    Gosub, CheckThisRegItem
    if ContinueRegSearch = n ; 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
IfInString, RegValue, %RegSearchTarget%
{
    MsgBox, 4, , The following match was found:`n%A_LoopRegKey%\%A_LoopRegSubKey%\%A_LoopRegName%`nValue = %RegValue%`n`nContinue?
    IfMsgBox, No
        ContinueRegSearch = n  ; Tell our caller to stop searching.
}
return