Re: search and replace in binary file
- From: "Pegasus \(MVP\)" <I.can@xxxxxxxxxx>
- Date: Mon, 10 Nov 2008 20:45:53 +0100
"Tony Logan" <TonyLogan@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:6D9CB9E9-DB42-458F-9F9C-1E54D3E2DDDB@xxxxxxxxxxxxxxxx
I'm trying to do a search and replace in a binary file, which is a
user-specific file used by a web-conferencing application. I'm trying to
preset some config settings that cause errors when the app is started
using
the default settings.
I've edited the file manually (in Notepad) to confirm the overall idea
works. The changes I need to make to the file are regular text edits, with
the exception of one character, which I'm not sure if it's a return
character, tab, etc. Looks like I can't attach files here, so best I can
do
is describe it: the non-text character looks like an open rectangle.
First I get the name of the logged in user so that data can be used for
building the path to the user-specific file I'm trying to edit.
After that I've tried 2 different approaches: reading through my file a
line
at a time, and also splitting it into an array. Both of these return only
a
single character, which is one of the funky rectangle characters, which is
the first thing in the file.
Based on what I've read online, there are some possible binary conversion
options, although I'm not at all clear on how these would work.
At any rate, here's the code I have:
Option Explicit
' declare variables
Dim objNetwork, objWMIService, objComputer, objFSO, objTextFile
Dim colComputer
Dim strComputer, strUser, strUserProfilePath, strNextLine, strFileText
Dim arrTokens, aDataFile
Dim nF
' set constants
Const ForReading = 1
Const ForWriting = 2
Set objNetwork = CreateObject("Wscript.Network")
strComputer = "."
Set objNetwork = CreateObject("WScript.Network")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")
' for each logged in user (there should only be 1),
' parse the string (ADMINS\username) to get just the username
For Each objComputer in colComputer
strUser = objComputer.UserName
arrTokens = split(strUser, "\")
strUser = arrTokens(1)
Next
' plug strUser into path to UserProfiles.dat so we can edit the file
strUserProfilePath = "C:\Documents and Settings\" & strUser &
"\Application
Data\Web Meeting\"
' check if UserProfiles.dat exists; if it does, open it and read through
it
Set objFSO = CreateObject("Scripting.FileSystemObject")
' check if file exists; if it does, open it
If objFSO.FileExists(strUserProfilePath & "UserProfiles.dat") Then
Set objTextFile = objFSO.OpenTextFile(strUserProfilePath &
"UserProfiles.dat", ForReading)
' read the whole file
If Not objTextFile.AtEndOfStream Then
strFileText = objTextFile.ReadAll
' split file into an array
aDataFile = Split(strFileText, vbCrLf)
' loop through each item in array, write out the lines
For nF = 0 To UBound(aDataFile)
WScript.Echo nF & ": " & strFileText
Next
End If
'OPTIONAL METHOD FOR DEALING WITH BINARY FILE,
' BUT GAVE SAME RESULT AS SPLITTING THE ARRAY
' loop through file, read and write it one line at a time
' Do Until objTextFile.AtEndOfStream
' strNextLine = objTextFile.ReadLine
' WScript.Echo strNextLine
' WScript.Echo " "
' Loop
End If ' (if file exists)
WScript.Quit
I'd obviously need to throw in some search/replace functionality, but I
have
working code to do that. First hurdle is trying to figure out if there's a
way to deal with the binary file in a way that vbs understands it.
The File System Object deals inherently with text files. If you're a purist
then you'll have to leave it at that: Searching and Replacing text in text
files is fine with VB Script, in binary files it is not. However, if you do
it just the right way then you can still do it. AFAIK the method is
undocumented and probably unsupported.
However, before you go down this track you should ascertain if your file is
a text or a binary file. Get yourself a proper binary viewer/editor so that
you can examine your file properly! XVI32.exe is one - it's free here:
www.chmaas.handshake.de.
.
- References:
- search and replace in binary file
- From: Tony Logan
- search and replace in binary file
- Prev by Date: search and replace in binary file
- Next by Date: Re: search and replace in binary file
- Previous by thread: search and replace in binary file
- Next by thread: Re: search and replace in binary file
- Index(es):
Relevant Pages
|