Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read .reg file

Status
Not open for further replies.

jeroen1

Technical User
Apr 25, 2005
12
NL
Hi,

I'm trying to write a script that reads .reg file and can compare it to the registry to see if there's a match.

I've got the registry checking part all done in another script, so in this one, I'm looking for reading the .reg file and creating strings that I can use in the regchecking function.

reading the .reg file is done:

Const ForReading = 1, ForWriting = 2, ForAppending = 8

' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

strFile = "U:\tst.reg"

If objFSO.FileExists(strFile) Then
Set objFile = objFSO.GetFile(strFile)
Set filetxt = objFile.OpenasTextStream(ForReading, -2)
Do While Not filetxt.atEndOfStream
line = filetxt.readline
wscript.echo line
Loop
Else
Wscript.Echo "File " & strFile & "doesn't exist"
End If

but now I'm looking for a way to recognize patterns, for example:

Windows Registry Editor Version 5.00 (ignore this sentence)

[HKEY_CURRENT_USER\Control Panel\Desktop] (this is the main key)
"FontSmoothing"="2" (how to get the sub key)
"FontSmoothingOrientation"=dword:00000001 (recognize DWORD values)
"SCRNSAVE.EXE"="C:\\WINDOWS\\System32\\logon.scr" (recognize backslashes)
"UserPreferencesMask"=hex:9e,3e,05,80 (recognize hex values)
"WaitToKillAppTimeout"="20000"
"Wallpaper"=""
"Pattern Upgrade"="TRUE"

[HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics] (recognize next main key, and process subkeys with corresponding main key)
"BorderWidth"="-15"
"CaptionFont"=hex:f5,ff,ff,ff,00,00,00,00,00,00,00,00,00,00,00,00,bc,02,00,00,\(recognize multi line strings)
00,00,00,00,00,00,00,00,54,00,61,00,68,00,6f,00,6d,00,61,00,00,00,00,00,00,\
00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\
00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00
"CaptionHeight"="-270"

idea's?

regards,

jeroen

 
is a .reg file format the best method for passing through data in your process? i can understand it is perhaps convient as you can output the registry easily with a command line tool.
 
well, I have this script that checks a server to see if an installation is done correctly (which is also done by a script). Since several applications are installed with added .reg files, I would like to check it like this, so I won't have to put all the loose registry entries in the check script.

regards
 
is your goal purely to determine if applications are installed? are there better methods for determining if an application is installed? WMI class perhaps? enumerating the registry with WMI enum_keys and enun_values?
 
No, my primary goal is to check if the server has all the correct registry keys. the servers are RISsed on a regular basis (terminal servers) and I would like to check if all installations happen correctly.

regards,

jeroen
 
OK, so why do you need to read a reg file? If you know the keys that need to be checked, then put them in a script that reads the registry and looks for the specified values.

Here is a simple example that just checks if a registry key exists, this does not care what the value is.

Code:
On Error Resume Next
Set WSHShell = wscript.CreateObject("Wscript.Shell")
WSHShell.RegRead("HKLM\SYSTEM\CurrentControlSet\Services\Notthere")
If err.number <> 0 Then
	WScript.echo "The key could not be found"
Else
	WScript.echo "The key exists!"
End if

Here is another example that checks to see if the boot dir is C:\.

Code:
On Error Resume Next
Set WSHShell = wscript.CreateObject("Wscript.Shell")
KeyValue = WSHShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup\BootDir")

If KeyValue = "C:\" Then
	WScript.Echo "Boot Directory is C Drive"
Else
	WScript.Echo "Boot Directory is " & KeyValue
End If




I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
well, I do know what reg keys need to be checked, but because there are several reg files that should be imported during installation, and I would like to keep the script flexible, it would be more suitable to read .reg files and scan accordingly, than to read the .reg files myself and put in an entry for each entry in the .reg files. If this could happen automatically it will be easy to add checks for new applications.

regards,

jeroen
 
Would it not suffice however to simply check for one key registry entry per application?

For example, if the program creates a key with it's name such as HKLM\Software\Company\ApplicationName.

If that key exists you know the install ran.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
true, but it does limit the flexibility.
I've started looking into Regular Expressions, which looks like the way to go.

what I'm not getting yet though, is how can I match a string between two characters? so for example, everything between ] and [?

regards,

jeroen
 
>what I'm not getting yet though, is how can I match a string between two characters? so for example, everything between ] and [?
[tt]
'rx the regular expression RegExp instance.
rx.pattern="\][^\[]*?\["
[/tt]
 
Hi,

I've had a little change in tactic, but I've allmost got it now:

Const ForReading = 1, ForWriting = 2, ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")
strFile = "U:\test.reg"

If objFSO.FileExists(strFile) Then
Set objFile = objFSO.GetFile(strFile)
Set filetxt = objFile.OpenasTextStream(ForReading, -2)
Do While Not filetxt.atEndOfStream
line = filetxt.readline
strQuote = instr(line, "]")

If strQuote = 0 Then
strSlash = right(line, 1)
strhex = instr(line, """")
'wscript.echo "slashvalue: " & strSlash
If strSlash = "\" or strHex = 0 Then
'wscript.echo "line contains slash: " & line
If line = "" Then
wscript.echo "empty line"
Else
sKey2 = sKey2 & line
End If
Else

sKey2 = sKey & line
strQuote = instr(line, "]")

End If
Else

sKey = line
End If
wscript.echo "Key is: " & sKey2
Loop

Else
Wscript.Echo "File " & strFile & "doesn't exist"
End If


only problems I have now is that if the line isn't a key (ie Windows Registry Editor Version 5.00) it will set the value to this. Also it will keep the value.
and at some points it will repeat when it shouldn't.

example output (that I have problems with):


Key is: Windows Registry Editor Version 5.00
empty line
Key is: Windows Registry Editor Version 5.00
Key is: Windows Registry Editor Version 5.00

Key is: [HKEY_CURRENT_USER\Control Panel\Desktop]"Pattern Upgrade"="TRUE"
empty line
Key is: [HKEY_CURRENT_USER\Control Panel\Desktop]"Pattern Upgrade"="TRUE"
Key is: [HKEY_CURRENT_USER\Control Panel\Desktop]"Pattern Upgrade"="TRUE"
Key is: [HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics]"BorderWidth"="-15"
Key is: [HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics]"BorderWidth"="-15""CaptionFont"=hex:f5,ff,ff,ff,00,00,00,00,00,00,00,00,00,00,00,00,bc,02,00,00,\
Key is: [HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics]"BorderWidth"="-15""CaptionFont"=hex:f5,ff,ff,ff,00,00,00,00,00,00,00,00,00,00,00,00,bc,02,00,00,\ 00,00,00,00,00,00,00,00,54,00,61,00,68,00,6f,00,6d,00,61,00,00,00,00,00,00,\
Key is: [HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics]"BorderWidth"="-15""CaptionFont"=hex:f5,ff,ff,ff,00,00,00,00,00,00,00,00,00,00,00,00,bc,02,00,00,\ 00,00,00,00,00,00,00,00,54,00,61,00,68,00,6f,00,6d,00,61,00,00,00,00,00,00,\ 00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\
Key is: [HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics]"BorderWidth"="-15""CaptionFont"=hex:f5,ff,ff,ff,00,00,00,00,00,00,00,00,00,00,00,00,bc,02,00,00,\ 00,00,00,00,00,00,00,00,54,00,61,00,68,00,6f,00,6d,00,61,00,00,00,00,00,00,\ 00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\ 00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00
Key is: [HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics]"CaptionHeight"="-270"
Key is: [HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics]"CaptionWidth"="-270"
Key is: [HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics]"CaptionWidth"="-270""IconFont"=hex:f5,ff,ff,ff,00,00,00,00,00,00,00,00,00,00,00,00,90,01,00,00,00,\
Key is: [HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics]"CaptionWidth"="-270""IconFont"=hex:f5,ff,ff,ff,00,00,00,00,00,00,00,00,00,00,00,00,90,01,00,00,00,\ 00,00,00,00,00,00,00,54,00,61,00,68,00,6f,00,6d,00,61,00,00,00,00,00,00,00,\
Key is: [HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics]"CaptionWidth"="-270""IconFont"=hex:f5,ff,ff,ff,00,00,00,00,00,00,00,00,00,00,00,00,90,01,00,00,00,\ 00,00,00,00,00,00,00,54,00,61,00,68,00,6f,00,6d,00,61,00,00,00,00,00,00,00,\ 00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\
Key is: [HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics]"CaptionWidth"="-270""IconFont"=hex:f5,ff,ff,ff,00,00,00,00,00,00,00,00,00,00,00,00,90,01,00,00,00,\ 00,00,00,00,00,00,00,54,00,61,00,68,00,6f,00,6d,00,61,00,00,00,00,00,00,00,\ 00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\ 00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00
Key is: [HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics]"IconSpacing"="-1125"

Hope this is example explains what I mean.

regards,

jeroen
 
Hi,



I've cleared out my design and started testing each part individually. This payed off, because I'm allmost there! :)



This is the current script:



Code:
Const ForReading = 1, ForWriting = 2, ForAppending = 8 

Set objFSO = CreateObject("Scripting.FileSystemObject")
strFile = "U:\test.reg"

If objFSO.FileExists(strFile) Then
 Set objFile = objFSO.GetFile(strFile)
 Set filetxt = objFile.OpenasTextStream(ForReading, -2) 
 Do While Not filetxt.atEndOfStream
  line = filetxt.readline
     
  strQuoteEqual =  instr(line, """=") 
  strhex = instr(line, """")
  strSlash = right(line, 1)
  strLBracket = left(line, 1)
  strRBracket = right(line, 1)
  strBracket = strLBracket & strRBracket
   
  If strBracket = "[]" Then
   strMainKey = line
   wscript.echo strMainKey
  Else
   
   If strQuoteEqual AND NOT strSlash = "\" Then
    strSubKey = line
   'Else 
   End If
   
   If strSlash = "\" or strHex = 0 Then 
    strHexKey = strHexKey & line
   Else 
    strKeyF = strHexKey
    strHexKey = ""
   End If 
   
   If strSubKey <> "" Then
   wscript.echo strMainKey & " " & strSubKey
   strSubKey = ""
   End If 
   
   If strKeyF <> "" Then
   wscript.echo strMainKey & " " & strKeyF
   End If
   
  End If
   
 Loop
    
Else
 Wscript.Echo "File " & strFile & "doesn't exist"
End If



The only thing I'm still experiencing is that, when I run the script it seems to be working fine, but after three lines it prints:



[HKEY_CURRENT_USER\Control Panel\Desktop]
[HKEY_CURRENT_USER\Control Panel\Desktop] "ActiveWndTrkTimeout"=dword:00000000
[HKEY_CURRENT_USER\Control Panel\Desktop] Windows Registry Editor Version 5.00 <=====
[HKEY_CURRENT_USER\Control Panel\Desktop] "AutoEndTasks"="0"
[HKEY_CURRENT_USER\Control Panel\Desktop] "CaretWidth"=dword:00000001



Can someone tell me why it prints the "WIndows Registry Editor 5.00" string and why on that particular spot?



regards,



jeroen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top