REG ADD
changes the entire entry. I have not been able to make REG ADD ~
work.
Windows registry contains multiple string zero values. This is what REG_MULTI_SZ
means. How can a new string be appended? REG ADD
deletes all string previously in the entry and writes only the new one there although REG_MULTI_SZ
is specified in the REG ADD
command.
Some say ~
before the string means append instead of overwrite. However, I do not what the exact syntax is. How can I use ~
?
[Edit]
Here is the simplified version of the question :
I have a registry entry KEYROOT\KEY\ENTRY
which is type REG_MULTI_SZ
. In this entry, there are string zeroes abcd\0
and efgh\0
. How can I add ijkl\0
in order to have all three: abcd, efgh and ijkl in the registry entry \ENTRY
and not only ijkl
?
I want to do this only with Batch Script or Command Prompt commands.
Please, note : I have tried many things with REG
and am unable to find a solution and I am not sure whether there is such.
I will be very thankful for any suggestions but I would like to ask you all to concentrate on the ~
possibility. Microsoft says when there is ~
before the string, the string is considered to be appended to the other strings and not to overwrite them.
I have tried many variations but am unable to get the correct syntax of ~
. Please, do provide information on how to use ~
before the string to be appended.
I found a solution. However, although the solution works, to add a string to the registry entry is not a good idea because Microsoft allows strings to be appended with hex data. Anyway, here is what I have been able to do :
- Read the value of the registry in a batch string variable :
FOR /F "usebackq tokens=2,* skip=2" %%G IN ('REG QUERY "Root\Key" /v Entry') DO SET EntryContents=%%H
Note the reverse apostrophes `.
Now, the entry of the registry key is in the variable EntryContents
.
- Concatenate the new value to the old value
SET NewEntryContents=%EntryContents%%ContentsToBeAppended%
The variable ContentsToBeAppended
has been set elsewhere in the batch file or may be a parameter. The variable NewEntryContents
contains the concatenated strings of the old registry key entry contents and the string which is in the variable ContentsToBeApended
.
- Add
NewEntryContents
to the entry with REG ADD.
Again, when the entry value is read into a string variable, all hex contents would be ignored in case they are 00's or other values which may be out of the character range. A solution may be to somehow export the entry with REG to a file in hex format and then to somehow add only hex values to the file. The string characters must be converted to their hex values and appended to the file as well as any new hex values.
This answer provides the ability to read a registry entry which would be read in full with all characters such as \0 in a string variable. The string variable will get all characters. Example : In case the registry entry contains PATH\NAME_OF_FILE\0PATH|NAME_OF_FILE
, all these will be read into the string variable. \0
too. Just concatenate the new string, which may also contain\0
's, and reg add the overall string back to the registry.
The trick here is the for /f
statement which skips 2 lines and takes the second string of the third line in G and the third ( the important one ) in H. All blanks and *'s, .'s, dits will be read too.
In order to add all blanks and *'s etcetera back to the entry with reg add, enclose the variable NewEntryContents
in quotes :
REG ADD "Root\Key" /v Entry /t REG_MULTI_SZ /d "%NewEntryContents%"
Hope this may help.
Also, the [~] sign works only when REGEDIT
is used. REGEDIT
is a better approach. Can save a key as is ( with hex ) in a file, then the file can be edited in the batch and then the newly edited key can be imported back to the registry. Pretty nasty, though. Also, I am not sure whether REGEDIT
would export the strings and the hex data combined to the file.
Here are the checks for the availability of the entry and for an empty entry :
- This checks whether the entry is there or not at all :
REG QUERY "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager" /v PendingFileRenameOperations
IF %ERRORLEVEL% EQU 0 GOTO THEREIS
- After the for
/f
:
SET "EntryContents"="%EntryContents: =%" IF DEFINED EntryContents GOTO NOTEMPTY
The SET line is not necessary. Gets rid of blanks around the string. This is supposed to be done by the for /f
anyway.
If defined checks the value. Best EntryContents
never to have been defined before the for /f
.
OK. I have managed to overcome all problems with batch script only.
Echo does echo all ASCII characters and can be made to output Unicode too, however, there are many more problems.
Anyway, I am unable to provide more information because of the amount of problems and solutions BUT, whoever is interested, can read the batch file I have made : http://www.steven-stanley-bayes.com/ForceDelete.zip