searching files for text

  • Thread starter Thread starter Bud Dean
  • Start date Start date
B

Bud Dean

I need to search files for given text. In particular, I'm searching dll's,
exe's, asp, aspx and html pages. I am having difficulty converting the byte
arrays to strings. The following code identifies the file and line number
where the text appears. It appears to work, but...

I would appreciate any suggestions for improvement.

heres the code:

*********************Start Code ****************************
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click


Dim boolFound As Boolean

'boolFound = findText("C:\Temp\EClaim.dll", "tblClmTrnUPRaw")

boolFound =
findText("D:\DotNet2003Projects\PMSSystem\ProviderMgmtSystemCR\bin\ProviderMgmtSystemCR.exe",
"tblProvMaster")

MessageBox.Show(boolFound)

End Sub

Function findText(ByVal sFile As String, ByVal sText As String) As Boolean

Dim Index As Long = 1

Dim str As String

Dim fi As FileInfo

Dim tr As BinaryReader

Dim ByteIndex As Long

Dim strArray() As String

Dim bArray() As Byte

Dim boolFound As Boolean = False

Try

fi = New FileInfo(sFile)

tr = New BinaryReader(File.OpenRead(fi.ToString))

bArray = tr.ReadBytes(fi.Length)

str = ConverToString(bArray)

strArray = Split(str, vbCrLf)

For Each str In strArray

ByteIndex = InStr(1, UCase(str), UCase(sText))

If ByteIndex > 0 Then

'MsgBox("Found In Standard encoding on line " & CStr(Index) & " - Byte:
" & CStr(ByteIndex))

'Console.WriteLine("-------------------Found Start------------")

'Console.WriteLine(str)

'Console.WriteLine("-------------------Found End------------")

boolFound = True

End If

Index += 1

Next

Return boolFound

tr.Close()

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

End Function

Function ConverToString(ByRef bArray As Byte()) As String

Dim b As Byte

Dim sb As New StringBuilder

Try

For Each b In bArray

If b <> 0 Then sb.Append(Chr(b))

Next

Return "" & sb.ToString

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

End Function



*********************END CODE*****************************



Thanks in advance for any help and guidance...



Bud Dean
 
I need to search files for given text. In particular, I'm searching dll's,
exe's, asp, aspx and html pages. I am having difficulty converting the byte
arrays to strings. The following code identifies the file and line number
where the text appears. It appears to work, but...

I would appreciate any suggestions for improvement.

heres the code:

*********************Start Code ****************************
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click


Dim boolFound As Boolean

'boolFound = findText("C:\Temp\EClaim.dll", "tblClmTrnUPRaw")

boolFound =
findText("D:\DotNet2003Projects\PMSSystem\ProviderMgmtSystemCR\bin\ProviderMgmtSystemCR.exe",
"tblProvMaster")

MessageBox.Show(boolFound)

End Sub

Function findText(ByVal sFile As String, ByVal sText As String) As Boolean

Dim Index As Long = 1

Dim str As String

Dim fi As FileInfo

Dim tr As BinaryReader

Dim ByteIndex As Long

Dim strArray() As String

Dim bArray() As Byte

Dim boolFound As Boolean = False

Try

fi = New FileInfo(sFile)

tr = New BinaryReader(File.OpenRead(fi.ToString))

bArray = tr.ReadBytes(fi.Length)

str = ConverToString(bArray)

strArray = Split(str, vbCrLf)

For Each str In strArray

ByteIndex = InStr(1, UCase(str), UCase(sText))

If ByteIndex > 0 Then

'MsgBox("Found In Standard encoding on line " & CStr(Index) & " - Byte:
" & CStr(ByteIndex))

'Console.WriteLine("-------------------Found Start------------")

'Console.WriteLine(str)

'Console.WriteLine("-------------------Found End------------")

boolFound = True

End If

Index += 1

Next

Return boolFound

tr.Close()

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

End Function

Function ConverToString(ByRef bArray As Byte()) As String

Dim b As Byte

Dim sb As New StringBuilder

Try

For Each b In bArray

If b <> 0 Then sb.Append(Chr(b))

Next

Return "" & sb.ToString

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

End Function



*********************END CODE*****************************



Thanks in advance for any help and guidance...



Bud Dean

Bud, check out the System.Text.Encoding class... You can do something
like:

' buffer is a byte array...
Dim s As String = Encoding.Default.GetString (Buffer)

HTH
 
Back
Top