Search for a string inside a binary file , replace it and save again the binary.

Hello ,

I am facing a problem and I don't Know the solution.

I'm using VB .Net 2005 . All I'm trying to do is to read a binary file which contains data with the format XX/XX/XXXX XX:XX , replace this value with something else and then save again the binary file.

I have written the following code.

Dim filepath as string = "C:\filetobeparsed.pbd"
Dim ifile As FileStream = New FileStream(filepath, FileMode.Open)
Dim lengthInBytes As Long = ifile.Length
Dim bin As BinaryReader = New BinaryReader(ifile)
Dim bytearray As Byte() = bin.ReadBytes(CInt(lengthInBytes))
Dim encEncoder As System.Text.Encoding = System.Text.ASCIIEncoding.ASCII
Dim str As String = encEncoder.GetString(bytearray)

str = str.ToUpper
str = str.Replace("XX/XX/XXXX XX:XX", "Test")
'I am writting this to a new txt file. Just to test.

Dim NewFile As String = ""
NewFile = FilePath.ToUpper
NewFile = NewFile.Replace(".PBD", ".TXT")

Dim fs As New FileStream(NewFile, FileMode.Create, FileAccess.Write)
Dim s As New StreamWriter(fs)
s.WriteLine(str)
s.Close()

Replace function does not work for me and I don't Know why. Also when I look at the 'str' variable which is supposed that contains the contents of the file I am getting only a very small part of the file. I am saving although the str as a txt and when I open it it contains the binary format of the file in a readable view. But I cannot process the file programatically and change the things that I want.



Try this. Create for eg a doc file . Write inside the doc the value XX/XX/XXXX XX:XX and then use the above code and try to change it.

Does anyone else has a better idea ? I want to read a binary file , locate a string with the format XX/XX/XXXX XX:XX inside it ,change it and then save again with the new value the binary file.

Also I am attaching the originall file that I am trying to use.
Check attachments.
The Zip file contains the file that I want to parse. The Txt file is the export of the file I am getting , with the above code that I have written.

I would be mostly appreciated if you have any sample code or correct mine.

Thank you

zkar
[2316 byte] By [zakkar] at [2007-11-20 11:11:14]
# 1 Re: Search for a string inside a binary file , replace it and save again the binary.
It's not going to work with any mass-read/encode attempts. The issue is that the file characters are not stored sequentially along byte boundaries, but in a proprietary format. You need to have an understanding of the file format in order to locate and "extract" the data you're looking for.
Craig Gemmill at 2007-11-10 3:08:25 >
# 2 Re: Search for a string inside a binary file , replace it and save again the binary.
Hi and thank you for your answer.

I couldn't know that. All I know is that I am having this formatted variable
XX/XX/XXXX XX:XX which I must allocate inside the binary file (only this format it will be-no duplicates) and replace this string with a today's date value. And of course save again the binary file with the new value.
I have to think also CRC check.

Any ideas ?

Thank you
zkar
zakkar at 2007-11-10 3:09:25 >