readin a free file and search words in it.

Posted on 2003-03-20
Medium Priority
Last Modified: 2010-05-01
He I want to read in a free file. and then check the text for words (search). if the word is in a paragraph the whole paragraph shall be written into another .txt file.
Info: paragraph beginnings are marked by an empty line or double space
Question by:jpwiedenhoff
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

saurabhgupta earned 120 total points
ID: 8177342
Hi jpwiedenhoff,

Private Function SearchPara(InData As String) As String
    Dim OutData As String   'This will store paragraphs to be written to the second file
    Dim strArray() As String
    strArray = Split(InData, vbCrLf)
    Dim i As Integer
    For i = 0 To UBound(strArray)
        If InStr(1, strArray(i), SEARCH_WORD) Then
            OutData = OutData & vbCrLf & strArray(i)
        End If
    SearchPara = OutData
End Function

This assumes that empty line means one vbCrLf.


Expert Comment

ID: 8180463

Private Function SelectParagraphs(ByVal InData As String, SearchWord As String) As String
Dim OutData As String, Paragraphs() As String, ParaID As Long
    'Changes double spaces to one line gap for processing only
    Let InData = Replace(InData, Space(2), vbCrLf & vbCrLf)
    'Split paragraphs
    Let Paragraphs = Split(InData, vbCrLf & vbCrLf)

    For ParaID = LBound(Paragraphs) To UBound(Paragraphs)
        If Not InStr(Paragraphs(ParaID), SearchWord) = 0 Then
            'Skip for first para to avoid line gap at first
            If Not OutData = vbNullString Then Let OutData = OutData & vbCrLf & vbCrLf
            Let OutData = OutData & Paragraphs(ParaID)
        End If
    Let SelectParagraphs = OutData
End Function

Private Sub DoAction(fnSource As String, fnDest As String, SearchWord As String)
Dim hFile As Integer
Dim Data As String
    Let hFile = FreeFile
    Open fnSource For Binary Access Read As hFile
        Let Data = Space(LOF(hFile))    'Allocate Memory
        Get hFile, , Data               'Read Data
    Close hFile
    Open fnDest For Binary As hFile
        Let Data = SelectParagraphs(Data, SearchWord)
        Put hFile, , Data               'Write data
    Close hFile
End Sub

Private Sub Form_Load()
    Call DoAction("D:\source.txt", "d:\dest.txt", "test")
End Sub

This example is as per your needs. i hope this minimized you work.
but this will be slow if the source file is huge. bcos this code reads the entire content to memory.


Expert Comment

ID: 8531917
Hi jpwiedenhoff,
This old question (QID 20557960) needs to be finalized -- accept an answer, split points, or get a refund.  Please see http://www.cityofangels.com/Experts/Closing.htm for information and options.

Expert Comment

ID: 8957409
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

-->Accept saurabhgupta 's comments as answer

Please leave any comments here within the next seven days.


EE Cleanup Volunteer

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month8 days, 3 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question