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

search and modify values

416 views
Skip to first unread message

Like_Shouhuo

unread,
Oct 13, 2003, 3:51:05 PM10/13/03
to
I need to replace all F:\ with G:\ in all values under HKEY_LOCAL_MACHINE\SOFTWARE\Software1 as we are moving the install folder to another server mapped under a different drive letter.

How can I, using VBScripts -

1. backup everything under HKEY_LOCAL_MACHINE\SOFTWARE\Software1 before modifying it, so that in case of any problem we can easily restore by merging the saved subtree?
2. search and replace all F:\ with G:\?

Thanks!!!

Torgeir Bakken (MVP)

unread,
Oct 13, 2003, 6:54:54 PM10/13/03
to
Like_Shouhuo wrote:

Hi

Here you go:


sKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Software1"

sBackupFile = "c:\Backup\Software1.reg" ' folder(s) must exist!

Const OpenAsASCII = 0
Const FailIfNotExist = 0
Const ForReading = 1
Const OverwriteIfExist = -1

Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")

' do not overwrite an existing file
If Not oFSO.FileExists(sBackupFile) Then
' export the registry key to a file
sCmd = "regedit.exe /S /E:A """ & sBackupFile & """ " & """" & sKey & """"
oShell.Run sCmd, 0, True
Else
WScript.Echo "Backup file alredy exists, will use existing file" _
& " as source for replace function"
End If

' open and read the registry file
Set fFile = oFSO.OpenTextFile(sBackupFile, ForReading, _
FailIfNotExist, OpenAsASCII)

sContent = fFile.ReadAll
fFile.Close

' replace the drive pointer
sContent = Replace(sContent, "F:\", "G:\", 1, -1, vbTextCompare)

' create a temporary registry file
sTemp = oShell.ExpandEnvironmentStrings("%TEMP%\")
sTempFile = sTemp & oFSO.GetTempName
Set fFile = oFSO.CreateTextFile(sTempFile, OverwriteIfExist, OpenAsASCII)
fFile.WriteLine sContent
fFile.Close

' import the registry file
oShell.Run "regedit.exe /S """ & sTempFile & """", 1, True

' delete it
oFSO.DeleteFile sTempFile

WScript.Echo "Finished"

--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of the 1328 page Scripting Guide: http://www.microsoft.com/technet/scriptcenter


Like_shouhuo

unread,
Oct 14, 2003, 2:16:04 PM10/14/03
to
Thank you! That is exactly what I need!!!
0 new messages