-
August 17th, 2004, 02:31 PM
#1
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.
-
August 17th, 2004, 05:21 PM
#2
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...
-
August 18th, 2004, 01:56 AM
#3
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?
-
August 18th, 2004, 08:29 AM
#4
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
-
August 19th, 2004, 02:45 PM
#5
Guys, I tried all your suggestions but it still don't work.
Can somebody suggext another one?
thanks a lot guys...
-
August 19th, 2004, 04:47 PM
#6
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?
-
August 19th, 2004, 05:35 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|