CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2004
    Posts
    4

    Exclamation Text File Processing

    I'm making a program that searches the word MAMPLASAN in a text file and save the lines that contains that word to another text file. I tried but I can't make the desired output. Can anybody help me make a procedure or a function that can accomplish this? Any help will be highly appreciated. Please...

    ===attached is a sample of the text file.
    Last edited by lowie82ph; August 18th, 2004 at 12:46 PM.

  2. #2
    Join Date
    Apr 2002
    Location
    Melbourne, Victoria, Australia
    Posts
    1,792
    Start a new project, put a command button on the form, add a reference to Microsoft Scripting Runtime (sccrun.dll) and add the following code - its a bit cludgy, but it works:
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    Dim fso As Scripting.FileSystemObject
    Dim fFile
    Dim fFileOut
    Dim fNameInput As String
    Dim fNameOutput As String
    Dim strInput As String
    fNameInput = "C:\Sample_Text.TXT"
    fNameOutput = "C:\Output.txt"
    Set fso = New Scripting.FileSystemObject
    
    Set fFile = fso.OpenTextFile(fNameInput, ForReading)
    Set fFileOut = fso.CreateTextFile(fNameOutput, True)
    Do While Err.Number = 0
        On Error Resume Next
        strInput = fFile.ReadLine
        If Err.Number = 0 Then
            If InStr(1, strInput, "MAMPLASAN", vbTextCompare) Then
                fFileOut.WriteLine (strInput)
            End If
        Else
            Err.Clear
            Exit Do
        End If
    Loop
    fFile.Close
    fFileOut.Close
    Set fFile = Nothing
    Set fFileOut = Nothing
    Set fso = Nothing
    End Sub
    Be nice to Harley riders...

  3. #3
    Join Date
    Dec 2001
    Posts
    6,332
    Here is a very fast way to do it. Note that very large files should not be loaded into memory all at once, but Split() and Filter() can still be used. I'd guess the typical system might safely load an entire file up to about a meg or so.
    Code:
    Dim A$, S$()
    
    Open "C:\file1.txt" For Binary Access Read As #1
      A = Space(LOF(1))
      Get #1, , A
    Close #1
    
    S = Split(A, vbCrLf)
    S = Filter(S, "MAMPLASAN", 1, 1)
    
    A = Join(S, vbCrLf)
    Open "C:\file2.txt" For Output As #1
      Print #1, A
    Close #1
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  4. #4
    Join Date
    Aug 1999
    Location
    VA
    Posts
    12

    An even more simple version....

    Actually, you can do this with 15 lines of code and process only one line at a time to eliminate any memory concerns. I've added comments to explain it as it goes.


    Code:
        Dim InputFileNum As Integer
        Dim OutputFileNum As Integer
        Dim Buffer As String
        
        ' Get the input file handle and open for input-only
        InputFileNum = FreeFile
        Open "SourceFile.txt" For Input As InputFileNum
        
        ' Get the output file handle and open/create the file
        OutputFileNum = FreeFile
        Open "OutputFile.txt" For Output As OutputFileNum
        
        ' Read each line in the input file one line at a time
        While Not EOF(InputFileNum)
            
            ' Reads one line of text
            Line Input #InputFileNum, Buffer
            
            ' Check to see if MAMPLASAN is contained in that line...this is NOT case sensitive
            ' Use vbBinaryCompare if you are looking for case sensitive
            If (InStr(1, Buffer, "MAMPLASAN", vbTextCompare) <> 0) Then
                ' If it is found, output it to the destination file
                Print #OutputFileNum, Buffer
            End If
        Wend
    
        Close #OutputFileNum
        Close #InputFileNum

  5. #5
    Join Date
    Aug 2004
    Posts
    4
    Guys, I tried all your suggestions but it still don't work.
    Can somebody suggext another one?

    thanks a lot guys...

  6. #6
    Join Date
    Dec 2001
    Posts
    6,332
    Please give some specifics on what didn't work. Also, the examples are for files in which each line is properly terminated with CrLf. Are you certain of the file format?
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  7. #7
    Join Date
    Apr 2002
    Location
    Melbourne, Victoria, Australia
    Posts
    1,792
    What do you mean by "it still don't work."? I tested mine (again) using the file you sent and it works fine.
    Be nice to Harley riders...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured