7. Using .Reg Files to Automate Registry Changes
The .reg files created
by the Export command in Registry Editor are plain text, suitable for
reading and editing in Notepad or any similar editor. Therefore, they
provide an alternative method for editing your registry. You can export a
section of the
registry, change it offline, and then merge it back into the registry.
Or you can add new keys, values, and data to the registry by creating a
.reg file from scratch and merging it. A .reg file is particularly
useful if you need to make the same changes to the registry of several
different computers. You can make and test your changes on one machine,
save the relevant part of the registry as a .reg file, and then
transport the file to the other machines that require it.
Figure 3
shows a .reg file. In this case, the file was exported from the
HKCU\Software\ Microsoft\Windows\CurrentVersion\Explorer\Advanced key,
shown in Figure 4.
7.1. Identifying the Elements of a .Reg File
As you review the examples shown in the two figures, note the following characteristics of .reg files:
Header line The file begins with the line Windows Registry Editor Version 5.00.
When you merge a .reg file into the registry, Registry Editor uses this
line to verify that the file contains registry data. Version 5 (the
version shipped with Windows 7) generates Unicode text files, which can
be used in Windows XP, Windows 2000, and Windows Vista, as well as
Windows 7. If you want to share registry data with a system running
Windows 95/98/Me or Windows NT, select the Win9x/NT4 Registration Files
option when you export the file in Registry Editor. To create from
scratch a .reg file that's suitable for import into Windows 95/98/Me or
Windows NT, use the header REGEDIT4 instead of Windows Registry Editor Version 5.00. Key names
Key names are delimited by brackets and must include the full path from
the root key to the current subkey. The root key name must not be
abbreviated. (Don't use HKCU, for example.) Figure 22-21 shows only one key name, but you can have as many as you please. The default value Undefined default values do not appear in .reg files. Defined default values
are identified by the special character @. Thus, a key whose default
REG_SZ value was defined as MyApp would appear in a .reg file this way: "@"="MyApp"
Value names
Value names must be enclosed in quotation marks, whether or not they
include space characters. Follow the value name with an equal sign. Data types REG_SZ values don't get a data type identifier or a colon. The data directly follows the equal sign. Other data types are identified as shown in Table 1: Table 1. Data Types Identified in .Reg FilesData Type | Identifier |
---|
REG_BINARY | hex | REG_DWORD | dword | REG_QWORD | hex(b) | REG_MULTI_SZ | hex(7) | REG_EXPAND_SZ | hex(2) |
A colon separates the identifier from the data. Thus, for example, a REG_DWORD value named Keyname with value data of 00000000 looks like this: "Keyname"=dword:00000000
REG_SZ values
Ordinary string values must be enclosed in quotation marks. A backslash
character within a string must be written as two backslashes. Thus, for
example, the path C:\Program Files\Microsoft Offce\ is written like
this: "C:\\Program Files\\Microsoft Office\\"
REG_DWORD values DWORD values are written as eight hexadecimal digits, without spaces or commas. Do not use the 0x prefix. All other data types
All other data types—including REG_EXPAND_SZ, REG_MULTI_ SZ, and
REG_QWORD—appear as comma-delimited lists of hexadecimal bytes (two hex
digits, a comma, two more hex digits, and so on). The following is an
example of a REG_MULTI_SZ value: "Addins"=hex(7):64,00,3a,00,5c,00,6c,00,6f,00,74,00,00,75,00,73,00,5c,00,\ 31,00,32,00,33,00,5c,00,61,00,64,00,64,00,64,00,69,00,6e,00,73,00,5c,00,\ 64,00,71,00,61,00,75,00,69,00,2e,00,31,00,32,00,61,00,00,00,00,00,00,00
Line-continuation characters
You can use the backslash as a line-continuation character. The
REG_MULTI_SZ value just shown, for example, is all one stream of bytes.
We've added backslashes and broken the lines for readability, and you
can do the same in your .reg files. Line spacing You can add blank lines for readability. Registry Editor ignores them. Comments To add a comment line to a .reg file, begin the line with a semicolon.
7.2. Using a .Reg File to Delete Registry Data
.Reg files are
most commonly used to modify existing registry data or add new data. But
you can also use them to delete existing values and keys.
To delete an existing
value, specify a hyphen character as the value's data. For example, to
use a .reg file to remove the value ThumbnailSize from the key
HKCU\Software\Microsoft\ Windows\CurrentVersion\Explorer, add the
following lines to the .reg file:
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer] "ThumbnailSize"=-
To delete an
existing key with all its values and data, insert a hyphen in front of
the key name (inside the left bracket). For example, to use a .reg file
to remove the key HKCR\.xyz\ shell and all its values, add the following
to the .reg file:
[-HKEY_CLASSES_ROOT\.xyz\shell]
7.3. Merging a .Reg File into the Registry
To merge a .reg file
into the registry from within Registry Editor, open the File menu and
click Import. Registry Editor adds the imported data under the
appropriate key names, overwriting existing values where necessary.
The default action for a .reg
file is Merge—meaning merge with the registry. Therefore, you can merge a
file into the registry by simply double-clicking it in Windows Explorer
and answering the confirmation prompt.
|