Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Rename reg keys

69 views
Skip to first unread message

tplane

unread,
Oct 31, 2002, 7:20:20 PM10/31/02
to
I need help to write a script to rename reg keys that already exist. I
have 2 different versions of the same application and they both use
the same reg keys.

ex. hkey_local_michine\software\key2002 and
hkey_local_michine\software\key2001

I found scripts to add, delete and change values but none that will
rename the keys. I want to rename the keys from one key name to the
other.

ex. ("key" to "key2001" back to "key")

This way I don't have to manaully go into the registry everytime I
want to use the other version of the application.

I hope that made since.

Thanks in advance.

Alex K. Angelopoulos (MVP)

unread,
Nov 2, 2002, 3:43:35 PM11/2/02
to
A simple method is to have a script do this:

(1) Silently export the key using regedit

(2) delete the old key

(3) modify the exported key

(4) import the new key.

This may not work instantly; you may have to wait for shell notification to
occur before the changes take effect.

A script is attached below. BE VERY, VERY, VERY, VERY CAREFUL!!!!!!!!!!

Misuse may cause injury or death.

In news:234a82bc.02103...@posting.google.com,
tplane typed:


oldkey = "HKEY_LOCAL_MACHINE\SOFTWARE\test1"
newkey = "HKEY_LOCAL_MACHINE\SOFTWARE\test2"

filTemp = "C:\tmp.tmp.reg"

' export using the RegeditRead function below
sData = RegeditRead(oldkey)


' change the path with the Replace function
sData = Replace(sData, oldkey, newkey)

' now import that into the registry using
' regeditwrite below

RegeditWrite sData

RemoveKey oldkey

Function RegeditRead(sKey)
' Exports sKey from registry to a variable
' PROCEDURE DEPENDENCIES: fRead
Dim Sh, sCmd, fTmp
With CreateObject("Scripting.FileSystemObject")
fTmp = .GetTempName
Set Sh = CreateObject("WScript.Shell")
sCmd = "regedit /s /e """ & fTmp & """ " & """" & sKey & """"
Sh.Run sCmd,0,True
RegeditRead = fRead(fTmp)
.DeleteFile fTmp
End With
End Function

Sub RegeditWrite(sData)
' imports Reg-file format data into the registry
Const ForWriting = 2
Dim header, FSO, sTmp, fTmp, tsTmp
Set FSO = CreateObject("Scripting.FileSystemObject")
sTmp = FSO.GetAbsolutePathName(FSO.GetTempName)
FSO.CreateTextFile(sTmp)
Set fTmp = FSO.GetFile(sTmp)
Set tsTmp = fTmp.OpenAsTextStream(ForWriting, True)
tsTmp.Write(sData): tsTmp.Close
CreateObject("WScript.Shell").Run "%COMSPEC% /C regedit /s " _
& Chr(34) & sTmp & Chr(34), 0, True
FSO.DeleteFile sTmp, True
End Sub

Sub RemoveKey(sKey)
' Removes Registry key and everything underneath it
Const ForWriting = 2
Dim header, sData, FSO, sTmp, fTmp, tsTmp
header = "REGEDIT4" & vbCrLf & vbCrLf
sData = header & "[-" & sKey & "]" & vbCrLf
Set FSO = CreateObject("Scripting.FileSystemObject")
sTmp = FSO.GetAbsolutePathName(FSO.GetTempName)
FSO.CreateTextFile(sTmp)
Set fTmp = FSO.GetFile(sTmp)
Set tsTmp = fTmp.OpenAsTextStream(ForWriting, True)
tsTmp.Write(sData): tsTmp.Close
CreateObject("WScript.Shell").Run "%COMSPEC% /C regedit /s " _
& Chr(34) & sTmp & Chr(34), 0, True
FSO.DeleteFile sTmp, True
End Sub

Function fRead(FilePath)
'Given the path to a file, will return entire contents
' works with either ANSI or Unicode
Dim FSO, CurrentFile
Const ForReading = 1, TristateUseDefault = -2, _
DoNotCreateFile = False
Set FSO = createobject("Scripting.FileSystemObject")
If FSO.FileExists(FilePath) Then
If FSO.GetFile(FilePath).Size>0 Then
Set CurrentFile = FSO.OpenTextFile(FilePath, ForReading, _
False, TristateUseDefault)
fRead = CurrentFile.ReadAll: CurrentFile.Close
End If
End If
End Function

--
Please respond in the newsgroup so everyone may benefit.
http://dev.remotenetworktechnology.com
----------
Subscribe to Microsoft's Security Bulletins:
http://www.microsoft.com/technet/security/bulletin/notify.asp

0 new messages