Opening and Closing a registry (hive) file
Opening a registry file requires assigning it to a number from 0 through 4. In the following example, the OPEN REG command is used to open reg.hiv and assign it to 2, and then CLOSE REG is used to close the file. When closing the file, the same number must be used as when opening it.
OPEN REG 2 reg.hiv
|
open registry file reg.hiv and assign it to 2
|
CLOSE REG 2
|
close registry file assigned to 2
|
Opening, Closing, and Listing registry keys
Once a registry hive file has been opened, most of the other registry commands will require that one or more keys be opened before anything further can be done. To open a key, the OPEN KEY command is used to assign a number from 0 through 19 to the key. The assigned number can then used in subsequent commands to reference that particular key in the registry file. To close the key, the CLOSE KEY command is used. The same number assigned when opening the key must be used to close it.
As an example, assume that a hive file named reg.hiv contains 2 keys named Key1 and Key2. The following sequence of commands can be used to open the registry file, open the entire hive as a key, list the keys in the hive, close the key, and then close the registry file.
OPEN REG 2 reg.hiv
|
open the registry file and assign it to 2
|
OPEN KEY 0 "" 2
|
open the entire hive as a key and assign it to 0
|
LIST KEYS 0
|
list the keys in the hive (will list Key1 and Key2)
|
CLOSE KEY 0
|
close the key assigned to 0
|
When you close a registry file, all open keys are automatically closed at the same time.
Therefore, it is not necessary to explicitly close all open keys first before closing a registry file. In addition, exiting from TBOSDT (or the completion of a script) will automatically close all open keys, as well as close all open registry files.
To open just Key1 (instead of the entire hive), the following sequence could be used instead:
OPEN REG 2 reg.hiv
|
open registry file and assign it to 2
|
OPEN KEY 0 Key1 2
|
open Key1 and assign it to 0
|
LIST KEYS 0
|
list the keys in Key1 (will list the subkeys of Key1)
|
CLOSE REG 2
|
close the hive file assigned to 2
|
If Key1 in the example above contained a subkey named SubKey1, and the goal was to open just SubKey1, that could be accomplished as follows:
OPEN REG 2 reg.hiv
|
open registry file and assign it to 2
|
OPEN KEY 0 Key1\SubKey1 2
|
open SubKey1 under Key1 and assign it to 0
|
LIST KEYS 0
|
list the keys in SubKey1
|
CLOSE REG 2
|
close the hive file assigned to 2
|
In addition, the OPEN KEY command’s /k option allows for a key to be opened by referencing an already opened key, instead of a registry file. This is demonstrated in the example below:
OPEN REG 2 reg.hiv
|
open registry file and assign it to 2
|
OPEN KEY 0 "" 2
|
open the entire hive as a key and assign it to 0
|
OPEN KEY 1 Key1 0 /k
|
open Key1 and assign it to 1 (the /k means the 0 is a key, not a file)
|
CLOSE REG 2
|
close the registry file assigned to 2
|
Adding and Deleting keys
Keys can be added with the ADD KEY command, and can be added with or without first opening a key. The following sequence of commands will create a new key named Key3 in the root of the hive, and then a new subkey of Key3 named SubKey1.
OPEN REG 2 reg.hiv
|
open registry file and assign it to 2
|
ADD KEY Key3 2
|
create Key3 in the root of the registry hive assigned to 2
|
ADD KEY Key3\SubKey1 2
|
create SubKey1 under Key3
|
CLOSE REG 2
|
close the registry file assigned to 2
|
A key can also be added by using the OPEN KEY command with the /c option. The /c option will create the key if it doesn’t already exist. The example below will create and open Key3:
OPEN REG 2 reg.hiv
|
open registry file and assign it to 2
|
OPEN KEY 0 Key3 2 /c
|
create Key3, open it, and assign it to 0
|
CLOSE REG 2
|
close the registry file assigned to 2
|
Deleting a key is accomplished with the DEL KEY command, and requires that a key first be opened (opening first is optional when adding a key). If a key contains one or more values, those will be deleted automatically when the key is deleted. If a key contains one or more subkeys, those can be deleted at the same time by using the /s option. Note that a DEL KEY command will fail if there are subkeys below it and the /s option is not used. In the following example, Key3 that was added in the example above is now deleted, along with any subkeys it contains:
OPEN REG 2 reg.hiv
|
open registry file and assign it to 2
|
OPEN KEY 0 ""
|
open the entire hive as a key and assign it to 0
|
DEL KEY 0 Key3 /s
|
delete Key3 and all subkeys below it (/s)
|
CLOSE REG 2
|
close the registry file assigned to 2
|
Copying and Renaming keys
An opened key can be renamed with the REN KEY command. The example below renames the key opened as Key3 to Key 4.
OPEN REG 2 reg.hiv
|
open registry file and assign it to 2
|
OPEN KEY 0 Key3 2
|
open Key3 and assign it to 0
|
REN KEY 0 Key4
|
rename Key3 to Key4
|
CLOSE REG 2
|
close the hive file assigned to 2
|
The COPY KEY command can copy a registry key by first opening both the key to be copied (the source), and the key to be copied to (the target). This is best demonstrated by the example below, which copies a key named Key3 from one registry file to another. When a key is copied, all subkeys and values in the key are copied along with it:
OPEN REG 2 reg1.hiv
|
open the source registry file, assign it to 2
|
OPEN KEY 0 Key3 2
|
open Key3 in the source registry file, assign it to 0
|
OPEN REG 3 reg2.hiv
|
open the target registry file, assign it to 3
|
OPEN KEY 1 "" 3
|
open the entire hive of reg2.hiv as a key, assign it to 1
|
COPY KEY 0 1 Key3
|
copy Key3 in reg1.hiv to reg2.hiv, and name the copy Key3
|
CLOSE REG 2
|
close the source registry file
|
In the example above, the copy of Key3 from reg1.hiv will now appear in the "root" of reg2.hiv, and it will be named Key3. To clarify, the copy of the key in reg2.hiv could have also been named Key4 (or any other name) by using the command COPY KEY 0 1 Key4 instead. Keys can be copied from one location to another within the same registry file, as well as from one registry file to another.
Listing Values names and Value data in a registry key
The values contained in a key (or subkey) can be listed by using the LIST VALUES command. The /d option can be used to display the value data in addition to the value names. The following sequence demonstrates how the LIST VALUES command can be used:
OPEN REG 2 reg.hiv
|
open registry file and assign it to 2
|
OPEN KEY 0 Key1 2
|
open Key1 and assign to 0
|
LIST VALUES 0
|
list value names in Key1
|
LIST VALUES 0 /d
|
list value names and value data in Key1
|
LIST VALUES 0 SubKey1 /d
|
list value names and data in SubKey1 under Key1
|
CLOSE REG 2
|
close the hive file assigned to 2
|
The LIST VALUE command is also available, and is used to display one particular value in a key or subkey. The following commands would display the data contained in the value named Value1.
OPEN REG 2 reg.hiv
|
open registry file and assign it to 2
|
OPEN KEY 0 Key1 2
|
open Key1 and assign to 0
|
LIST VALUE 0 Value1
|
display data in Value1 in Key1
|
LIST VALUE 0 SubKey1 Value1
|
display data in Value1 in Subkey1
|
CLOSE REG 2
|
close the hive file assigned to 2
|
Setting a Value in a registry key
The SET VALUE command can be used to either modify existing values in a key, or to create new ones. The following sequence of commands will first create a new value named Value1 in Key1, and then modify the value of Value1.
OPEN REG 2 reg.hiv
|
open registry file and assign it to 2
|
OPEN KEY 0 "" 2
|
open the entire hive as a key and assign it to 0
|
SET VALUE 0 Key1 Value1 sz string1
|
create Key1/Value1 of type sz with a value of "string1"
|
SET VALUE 0 Key1 Value1 sz string2
|
change data in Value1 to "string2"
|
CLOSE REG 2
|
close the hive file assigned to 2
|
Deleting a Value in a registry key
To delete a value, the DEL VALUE command is used. This command will only work on values in the open key itself (does not work on subkeys). The following sequence of commands will delete Value1 in Key1.
OPEN REG 2 reg.hiv
|
open registry file and assign it to 2
|
OPEN KEY 0 Key1 2
|
open Key1 and assign it to 0
|
DEL VALUE 0 Value1
|
delete Value1
|
CLOSE REG 2
|
close the hive file assigned to 2
|
Adding or Removing a string from a multi-string registry value
SET MSZVALUE is a special command to work with multi-string registry values. It can add/delete a string based on search criterion similar to the SET TEXTLINE command. The following examples use SET MSZVALUE to add/delete strings in a multi-string value named V1 in a key named Key1:
OPEN KEY 0 Key1 2
|
open Key1 and assign to 0
|
LIST VALUES 0 /d
|
list all value names and their values in Key1
|
SET MSZVALUE 0 "" V1 "xyz"
|
adds string "xyz" as the last string
|
SET MSZVALUE 0 "" V1 "xyz" /a
|
same as above
|
SET MSZVALUE 0 "" V1 "abc" /b
|
adds string "abc" as the first string
|
SET MSZVALUE 0 "" V1 "abc" /b "def"
|
adds string "abc" before string "def"
|
SET MSZVALUE 0 "" V1 "abc" /a "def"
|
adds string "abc" after string "def"
|
SET MSZVALUE 0 "" V1 "abc" /d
|
deletes string "abc"
|
SET MSZVALUE 0 "" V1 "Hello World" /d
|
deletes string "Hello World"
|
Importing and Exporting registry keys
A registry key branch can be exported from an open registry hive file by using the REGEXPORT command. The exported file will be in reg file format (text). Similarly, reg files can be imported into a registry hive file by using the REGIMPORT command. In the following example, the registry key branch named Key3 is exported to a file named key3.reg, and then that file is imported into another registry file:
OPEN REG 2 reg.hiv
|
open registry file reg.hiv and assign it to 2
|
OPEN KEY 0 Key3 2
|
open Key3 and assign it to 0
|
REGEXPORT key3.reg 0
|
export Key3 to a file named key3.reg
|
OPEN REG 3 reg2.hiv
|
open registry file reg2.hiv and assign it to 3
|
REGIMPORT key3.reg 3
|
import file key3.reg to registry file reg2.hiv
|
CLOSE REG 2
|
close the hive file assigned to 2
|
CLOSE REG 3
|
close the hive file assigned to 3
|
Note that a key must first be opened before exporting it, while keys are imported into a registry file by just opening the hive file, and then importing the key branch into the hive. The REGIMPORT and REGEXPORT commands support the REGEDIT4 (ANSI) format for registry files, but not REGEDIT5 (Unicode). Files imported with REGIMPORT must be in REGEDIT4 format.