Search a string inside a string

zakkar

Member
Joined
Oct 2, 2007
Messages
10
Programming Experience
Beginner
Hi ,

I have a file which contains a specific format text.
I have already open the file , read it etc...
Inside this file there is a a block of chars which have the following format
XX/XX/XXXX XX:XX

I want to replace the XX...etc with something else and save the file with the new value.
I have tried the replace function , I have tried with the regex but I cannot make it work.
I am attaching the file that I want to change. If you can help me I would be mostly appreciated.
Any ideas ? Any working example ?

Thank you
zkar
 

Attachments

  • MAINTAINFUNCTIONS.TXT
    7 KB · Views: 22
Hi ,

I have already did that.

I used the following code

VB.NET:
   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.Replace("XX/XX/XXXX XX:XX", "01/01/2007 13:23")
        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()
        ifile.Close()

But Replace is not working for me.
Any ideas ?
Thank you
zkar
 
Last edited by a moderator:
Search string inside a string

Hi ,
with the below line of code I am reading the file convert it to a string and fill the str variable. If I have this str variable written in a text file I can see in a readable view the contents of my binary file.

But. I cannot search inside this str variable and replace the value with the one I want.

Dim str As String = encEncoder.GetString(bytearray)
str = str.Replace("XX/XX/XXXX XX:XX", "01/01/2007 13:23")

That is the problem I am facing.
Every loop I make , every search , indexof I used nothing.
Only reads the first line of this str variable and then it stops the search. This is perhaps it is containg control characters wildcards etc...
But If I write this str in my HDD as a txt file I can see the contents.
Try it and you will see.
Any ideas ?

Thank you
zkar
 
You arent going to succeed with this because the file is mainly unicode or something like it, but youre grabbing it with an ascii encoding

To whit, here is the section youre looking to replace:

VB.NET:
000018c0h: 00 00 00 00 00 00 00 05 01 00 3E 00 00 00 00 00 ; ..........>.....
000018d0h: 00 00 20 00 20 00 20 00 20 00 20 00 20 00 20 00 ; .. . . . . . . .
000018e0h: 00 00 58 00 58 00 2F 00 58 00 58 00 58 00 2F 00 ; ..X.X./.X.X.X./.
000018f0h: 58 00 58 00 58 00 58 00 20 00 58 00 58 00 3A 00 ; X.X.X.X. .X.X.:.
00001900h: 58 00 58 00 00 00 36 00 2E 00 30 00 00 00 00 00 ; X.X...6...0.....
00001910h: 13 00 00 00 01 3F 10 00 32 00 0B 00 36 00 00 00 ; .....?..2...6...
00001920h: 00 00 00 00 20 00 20 00 20 00 20 00 20 00 20 00 ; .... . . . . . .

You CAN NOT call replace "XX/XXX/XXXX" with "yy/yyy/yyyy" and hope it will work

This file is effectively binary. It is NOT a text file. None of the solutions propsed here will work without damaging the file.. Youre gonna have to edit this file as a stream of bytes, I think. Read the whole file into a byte array, and either skip through it (or if it always occurs in the same place, use that index 0x000018E2) looking for an X, then check if that is followed by .X.X./.X blah blah, and replace them with the bytes you require, in situ
 
Search string inside a string

Hi ,

I used the following code and it worked .

VB.NET:
Public function Start() as Int16
Dim FilePath As String
    Dim TextToReplace As String
    Dim uniencoder As New UnicodeEncoding
    Dim Find1() As Byte = uniencoder.GetBytes("XX/XX/XXXX XX:XX")
    Dim FileOut As String

         Dim i As Integer = 0

        FilePath = "C:\test.doc"


        Dim TodaysDate As DateTime
        Dim StringDate As String

        TodaysDate = DateTime.Now()
        StringDate = Left(TodaysDate.ToString, 16)
        Dim Replace1() As Byte = uniencoder.GetBytes(StringDate)

        FileOut = FilePath
        Dim Well As Boolean = DoReplace(FilePath, Find1, Replace1, FileOut)

return 0
End Function

Private Function DoReplace(ByVal InPutFile As String, ByVal SearchArray As Byte(), _
ByVal ReplaceArray As Byte(), ByVal OutPutFile As String) As Boolean
        Try
            Dim FilesArray() As Byte = System.IO.File.ReadAllBytes(InPutFile)
            Dim I, J, IMax, JMax As Integer
            JMax = SearchArray.Length
            IMax = FilesArray.Length - JMax
            JMax -= 1
            For I = 0 To IMax
                For J = 0 To JMax
                    If Not (SearchArray(J) = FilesArray(I + J)) Then Exit For
                Next
                If J > JMax Then
                    For J = 0 To JMax
                        FilesArray(I + J) = ReplaceArray(J)
                    Next
                End If
                I += J
            Next
            System.IO.File.WriteAllBytes(OutPutFile, FilesArray)
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
 
Last edited by a moderator:
Back
Top