Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


How can you edit a .REG file with vbscript?

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,33084
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> How can you edit a .REG file with vbscript?
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 How can you edit a .REG file with vbscript? - 4/4/2006 8:12:22 AM   
  gwong

 

Posts: 4
Score: 0
Joined: 7/11/2005
Status: offline
I though this would be a simple process, like editing a text documnet, but apparently this is not the case.  The fast and sloppy code:

Option Explicit
Dim oShell,sInput,f,fs
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Set oShell = CreateObject("WScript.Shell")
Set fs = CreateObject("Scripting.FileSystemObject")

Set f = fs.OpenTextFile("C:\RegValue.reg",ForReading,True)
sInput = f.ReadAll
WScript.Echo sInput
If InStr(sInput,"\TEMPHIVE\") Then
sInput = Replace(sInput,"HKEY_LOCAL_MACHINE\TEMPHIVE","HKEY_CURRENT_USER")
End If

This is just a test; I am trying to determine why I cannnot read the .REG file properly.  Whenever the script runs, it returns 3 characters of gibberish - that's it.  Can someone point me in the right direction?

The contents of the "C:\RegValue.reg" file:
______________________________________________________________________________

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\TEMPHIVE\SOFTWARE\MICROSOFT\INTERNET EXPLORER\TYPEDURLS]
"url1"="http://www.microsoft.com/isapi/redir.dll?prd=ie&pver=6&ar=msnhome"
______________________________________________________________________________
 
 
Post #: 1
 
 RE: How can you edit a .REG file with vbscript? - 4/4/2006 12:15:33 PM   
  Cybex


Posts: 412
Score: 0
Joined: 9/14/2005
From: Florida
Status: offline
What is it you are trying to do?  Why not just read the registry and edit it directly?

As far as your code...
You are trying to open ir for reading but you never read it until at end of stream.
You are going to have to read it line by line.
You then try to write to something you opened for reading.

These are just a few...
If you just want to do it, it can be done fairly easy. Take another stab at it and post your updated code.


Cybex

_____________________________

Common sense is not so common.

(in reply to gwong)
 
 
Post #: 2
 
 RE: How can you edit a .REG file with vbscript? - 4/4/2006 3:56:28 PM   
  gwong

 

Posts: 4
Score: 0
Joined: 7/11/2005
Status: offline
Below is the code reading the file line by line as you suggested.  Same result.  3 characters of gibberish and nothing else.

Option Explicit
Dim oShell,sInput,f,fs
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Set oShell = CreateObject("WScript.Shell")
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("C:\RegValue.reg",ForReading,True)

Do While f.AtEndOfStream <> True
   sInput = f.ReadLine
   WScript.Echo "Original Input: " & sInput
   If InStr(sInput,"\TEMPHIVE\") Then
       sInput = Replace(sInput,"HKEY_LOCAL_MACHINE\TEMPHIVE","HKEY_CURRENT_USER")
   End If
   WScript.Echo "Corrected Input: " & sInput
Loop

This is a small part of a tool I am writing to backup user data.  In the tool, I am mounting the user's registry hive to a temporary location so I may access their settings.  In order to mount their profile, I am using REG.EXE /LOAD and mounting the file to HKLM\TEMPHIVE.  The issue is, when I export the settings, the registry key reads just as you see it "HKLM\TEMPHIVE\<KEY>\<KEY>" and I would like to automatically replace the "HKLM\TEMPHIVE" to "HKEY_CURRENT_USER" so that when the .REG file is imported on the new machine, the settings return to the correct location.  I have broken out this code from the main tool in order to troubleshoot why I am unable to read the text from a .REG file.

(in reply to Cybex)
 
 
Post #: 3
 
 RE: How can you edit a .REG file with vbscript? - 4/4/2006 4:56:04 PM   
  DiGiTAL.SkReAM


Posts: 1193
Score: 7
Joined: 9/6/2005
From: Florida, USA
Status: offline
Heya, gwong.  The problem here is that REGEDIT dumps file as unicode by default, so you have to open them AS unicode when reading htem in VBScript.
For example:

      

For more detailed info on openTextFile, go to http://www.devguru.com/technologies/vbscript/quickref/filesystemobject_opentextfile.html

EDIT: Minor typo fix.

_____________________________

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

"It is better to die like a tiger, than to live like a pussy."
-Master Wong, from Balls of Fury

(in reply to gwong)
 
 
Post #: 4
 
 RE: How can you edit a .REG file with vbscript? - 4/5/2006 3:28:56 AM   
  gwong

 

Posts: 4
Score: 0
Joined: 7/11/2005
Status: offline
Thanks!  I was missing the fact it was Unicode. (Pardon my ignorance)

The newest code:


      

My question is this - is there a way to manipulate strings in Unicode on the fly?  Writing to a text document in order to manipulate strings seems to be an extra step that I should be able to skip. 

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 5
 
 RE: How can you edit a .REG file with vbscript? - 4/5/2006 3:49:04 AM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: offline
Do you then turn around and import the reg file? If that is the case, then I would recommend writing directly to the value in the registry and leave a file out of it altogether.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to gwong)
 
 
Post #: 6
 
 RE: How can you edit a .REG file with vbscript? - 4/5/2006 4:12:45 AM   
  ehvbs

 

Posts: 2220
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi gwong,

VBScript strings are unicode strings, so this


      

works for me (and should work for you). Please check the params of the Replace function
carefully. Of course you may overwrite the source file (perhaps after some tests).

Good luck.

(in reply to ebgreen)
 
 
Post #: 7
 
 RE: How can you edit a .REG file with vbscript? - 4/5/2006 4:17:25 AM   
  gwong

 

Posts: 4
Score: 0
Joined: 7/11/2005
Status: offline
To answer your question, the value is coming from the user's old PC, and being restored to a different system.

(in reply to ehvbs)
 
 
Post #: 8
 
 RE: How can you edit a .REG file with vbscript? - 4/5/2006 4:18:43 AM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: offline
Are both machines accessible when this is run?

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to gwong)
 
 
Post #: 9
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> How can you edit a .REG file with vbscript? Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts