how i can check any text string in a doc file.the searchinh should be fast as i have to do other code if it found the string

Recommended Answers

All 15 Replies

Are you talking about a file on disk or loaded into a richtext or text box? The latter two can be done with the InStr function.
Unles you want to load the file into memory the first is a lot harder and slower to do.

Are you talking about a file on disk or loaded into a richtext or text box? The latter two can be done with the InStr function.
Unles you want to load the file into memory the first is a lot harder and slower to do.

i m talking abt file on disk.i want to search the text in the file content.any solution.if it slow might be work but at least give me solution

thanks

how i can check any text string in a doc file.the searchinh should be fast as i have to do other code if it found the string

u can do one thing...

open the file/doc in input mode...
read line by line...
in every check weather ur string is present using instr(line,string)
it returns value > 0 if string is found.

u can do one thing...

open the file/doc in input mode...
read line by line...
in every check weather ur string is present using instr(line,string)
it returns value > 0 if string is found.

i hav tried tat logic may be my code is wrong.can u provide me the code.frm ur logic it seems tat u r not a .net programmer.

Member Avatar for iamthwee

>can u provide me the code.

We do not just provide code. If you are too lazy to even try then you might as well give up programming altogether.

Send each filename and string you want to find to this function. It will return true if the string is found and false if the string is not found.

Private Function CheckFile(ByVal FileName As String, ByVal strSearch As String) As Boolean
        Dim s As String = My.Computer.FileSystem.ReadAllText(FileName)
        If InStr(s, strSearch) Then
            Return True
        Else
            Return False
        End If
    End Function

Send each filename and string you want to find to this function. It will return true if the string is found and false if the string is not found.

Private Function CheckFile(ByVal FileName As String, ByVal strSearch As String) As Boolean
        Dim s As String = My.Computer.FileSystem.ReadAllText(FileName)
        If InStr(s, strSearch) Then
            Return True
        Else
            Return False
        End If
    End Function

there is no such method filesystem.readalltext in framewrok1.1.how can i read file in string format?

Send each filename and string you want to find to this function. It will return true if the string is found and false if the string is not found.

Private Function CheckFile(ByVal FileName As String, ByVal strSearch As String) As Boolean
        Dim s As String = My.Computer.FileSystem.ReadAllText(FileName)
        If InStr(s, strSearch) Then
            Return True
        Else
            Return False
        End If
    End Function

What happen if the file size is 100 MB ?

I don't know about 100mb you might try it. If it doesn't work then you will have to do it as wavalker said:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If FindStringInFile("c:\test.txt", "gooder") Then
            MessageBox.Show("found")
        Else
            MessageBox.Show("not found")
        End If
    End Sub

    Public Function FindStringInFile(ByVal Filename As String, ByVal SearchString As String) As Boolean
        Dim Reader As System.IO.StreamReader
        Reader = New IO.StreamReader(Filename)
        Dim stringReader As String
        Try
            While Reader.Peek <> -1
                stringReader = Reader.ReadLine()
                If InStr(stringReader, SearchString) > 0 Then Return True
            End While
            Reader.Close()
            Return False
        Catch ex As Exception
            MessageBox.Show("Exception: " & ex.Message)
            Return False
        End Try
    End Function

Hopefully this will work in vb 2003 as I only have vb 2005.

i m talking abt the .doc Extension files i.e Microsoft word files not .txt files.

Ah, then you must connect to the document using word document or convert the word doc to a text file.
I don't have Word so I can't tell you how to do it. Word has a way to search in its files (I think).

thank you, but not worked for me. I want to search in a binary file (there are no lines). Do you have a solution? :D

Post a copy of the file, but not a 100mb one!:) Let me look at it and get back to you.

@waynespangler I have a question for you.
Say I search for "Money=" and it is found in the file, how would I display the contents after the "="? Say it was Money=500 in the file. So I want to search for "Money=" and then display the 500 in a text box. Any help would be appreciated.

Dim sReader As New StreamReader("C:\work document file name.doc")
        Dim text As String = sReader.ReadToEnd()
        sReader.Close()

        If text.Contains("word to search you can also use textbox") Then
            MessageBox.Show("show the searched data here")
        End If

Dont forget to import....

Imports System.IO
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.