Loop (registry)

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

Loop, RootKey [, Key, IncludeSubkeys?, Recurse?]





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.

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.



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) or if the operating system is Win9x (since Win9x does not track this info).


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



; Example: Delete Internet Explorer's history of URLs typed by the user: Loop, HKEY_CURRENT_USER, Software\Microsoft\Internet Explorer\TypedURLs
; Example: A working test script: Loop, HKEY_CURRENT_USER, Software\Microsoft\Windows, 1, 1 { if a_LoopRegType = key value = else { RegRead, value if ErrorLevel <> 0 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, HKEY_LOCAL_MACHINE, , 1, 1 { Gosub, CheckThisRegItem if ContinueRegSearch = n ; It told us to stop. return } Loop, HKEY_USERS, , 1, 1 { Gosub, CheckThisRegItem if ContinueRegSearch = n ; It told us to stop. return } Loop, HKEY_CURRENT_CONFIG, , 1, 1 { 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 <> 0 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