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

Dani AI

Generated

Short, practical summary and paths forward (for and others)

Reading a .txt with StreamReader/InStr (as and suggested) works fine — but a Word .doc is a binary Office file, not plain text, so treating it as a text stream will often fail or miss content. Use a real Word parser or convert the document first. (learn.microsoft.com)

Recommended approaches (choose one by environment)

  • If files are .docx: use the Open XML SDK to read the document XML (no Word install required). This is the fastest, supported server-side option for OOXML files. (learn.microsoft.com)

  • If files are legacy .doc and the code runs on a desktop client where Word is installed: Office automation (Microsoft.Office.Interop.Word) can open the document and read Document.Content.Text. Don’t run Interop from ASP/ASP.NET on a server — Microsoft explicitly warns against unattended server-side Automation of Office. If you go this route, open read-only, keep Word invisible, release COM objects and force GC to avoid leaks. Example (VB.NET client app):

Imports Microsoft.Office.Interop
Imports System.Runtime.InteropServices

Function DocContains(path As String, search As String) As Boolean
    Dim app As Word.Application = Nothing
    Dim doc As Word.Document = Nothing
    Try
        app = New Word.Application With {.Visible = False}
        doc = app.Documents.Open(path, ReadOnly:=True, AddToRecentFiles:=False)
        Return doc.Content.Text.IndexOf(search, StringComparison.OrdinalIgnoreCase) >= 0
    Finally
        If doc IsNot Nothing Then doc.Close(False) : Marshal.ReleaseComObject(doc)
        If app IsNot Nothing Then app.Quit(False) : Marshal.ReleaseComObject(app)
        GC.Collect() : GC.WaitForPendingFinalizers()
    End Try
End Function

Be careful: Interop is unsupported for unattended server use. (support.microsoft.com)

Server/high-volume or “no Word installed” options

  • Use a library that understands .doc without Word: Apache POI (Java) or the .NET port NPOI can extract text from .doc; commercial options (Aspose.Words, Syncfusion DocIO, etc.) offer robust server-safe APIs. For bulk work, convert to plain text (Apache Tika, antiword) or build a text index. For very fast file-content queries, register/use Windows Search / IFilter or build a Lucene/Elastic index over extracted text instead of opening every file at query time. (poi.apache.org)

Quick tip that answers (extract value after “=”)
After you extract the document text, find the token and take the substring:

Dim i = content.IndexOf("Money=")
If i >= 0 Then
  Dim start = i + "Money=".Length
  Dim value = content.Substring(start).TrimStart().Split({vbCr, vbLf, " "}, StringSplitOptions.RemoveEmptyEntries)(0)
End If

If you’ll search many files or very large files (100MB+ as asked), pre-extract and index text rather than repeatedly opening files.

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 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 Member #46692

>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.

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.