Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

readin a free file and search words in it.

Posted on 2003-03-20
4
Medium Priority
?
158 Views
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
0
Comment
Question by:jpwiedenhoff
4 Comments
 
LVL 2

Accepted Solution

by:
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
    Next
    SearchPara = OutData
End Function

This assumes that empty line means one vbCrLf.

Regards
Saurabh
0
 
LVL 5

Expert Comment

by:Rubyn
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
    Next
    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.

:)
0
 

Expert Comment

by:CleanupPing
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.
0
 
LVL 6

Expert Comment

by:GPrentice00
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.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

GPrentice00
EE Cleanup Volunteer
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

579 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