473,326 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

searching files for text

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\Provider MgmtSystemCR\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
Nov 21 '05 #1
1 1438
In article <uI**************@TK2MSFTNGP09.phx.gbl>, Bud Dean wrote:
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\Provider MgmtSystemCR\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
--
Tom Shelton [MVP]
Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John | last post by:
Hi everyone ! This is a first time I post a message here. If I post my message in a wrong group. Please ignore it. I am trying to build a website which allows users (can be thousands of...
2
by: Kakarot | last post by:
I'm gona be very honest here, I suck at programming, *especially* at C++. It's funny because I actually like the idea of programming ... normally what I like I'm atleast decent at. But C++ is a...
3
by: hivie | last post by:
I have a problem that is causing me problems. I have a text file that stores 5 lines of crap (stuff that I dont need( for the user only)). After that there is data that is in three columns...
1
by: Dave S | last post by:
Hello all, Hope this is the correct group for this question. I couldn't find a vb beginners group. I am getting overwhelmed and need some help. I did this very easily in a batch file (only three...
2
by: Mark Reed | last post by:
Hi all, I don't know if this is do-able but you never know until you ask. A situation at work has arisen where I have to trawl through over 500 text files, copy the contents and then paste them...
0
by: hatcat | last post by:
Hi everyone. I'm looking for the right tool for a job. Hope someone can help. The deal is this: We have a large amount of data which is maintained by non-programmers. This is validated and...
4
by: Hunk | last post by:
Hi I have a binary file which contains records sorted by Identifiers which are strings. The Identifiers are stored in ascending order. I would have to write a routine to give the record given...
15
by: spazzo6281 | last post by:
Hello, is there a way to search a folder for certain files containing a wildcard or certain text entered into a text box. Right now all i can get is the whole list of files from a folder using the...
12
by: Alexnb | last post by:
This is similar to my last post, but a little different. Here is what I would like to do. Lets say I have a text file. The contents look like this, only there is A LOT of the same thing. () A...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.