Code for searching & copying Text from 1 text file to another

G

Guest

This is quite out of my league. I am trying to get a set of code that would:
1. In a text file, Search line by line for a keyword or number (most likely
part of a sentence)
2. From the line where the 1st keyword/number was found, search again line
by line for another keyword/number
3. When the 2nd keyword is found, copy the contents between the lines
containing the two keywords to another text file
4. Loop the process for N sets of keywords

Thanks
 
G

Guest

http://msdn.microsoft.com/library/en-us/dnofftalk/html/office09072000.asp
Working with Files, Folders and Drives: More VBA Tips and Tricks by David
Shank

http://support.microsoft.com/default.aspx?scid=kb;en-us;151262
Working with Sequential Access Files

http://www.applecore99.com/gen/gen029.asp

Reading and Writing
http://msdn2.microsoft.com/en-us/library/czxefwt8.aspx

----------- Sample code to read a file --------------

Sub ReadStraightTextFile()
Dim sStr as String
Dim LineofText As String
Dim rw as Long
rw = 0
Open "C:\FILEIO\TEXTFILE.TXT" For Input As #1
sStr = ""
Do While Not EOF(1)
Line Input #1, LineofText
sStr = sStr & lineofText
if len(sStr) >= 178 then
rw = rw + 1
cells(rw,1).Value = sStr
sStr = ""
End if
Loop
'Close the file
if len(sStr) > 0 then
cells(rw,1).Value = sStr
End if
Close #1
End Sub

------- Some code by Jake Marx --------
function to truncate a file at a given test string (including the test string)

Private Const mlTEMP_FOLDER As Long = 2

Public Function gbTruncateFile(rsFullPath As String, _
rsTruncString As String) As Boolean
Dim fso As Object
Dim tsSrc As Object
Dim tsDest As Object
Dim bDone As Boolean
Dim sTemp As String
Dim lTruncPos As Long
Dim sDestPath As String

On Error GoTo ErrHandler

Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(rsFullPath) Then
Set tsSrc = fso.OpenTextFile(rsFullPath)
sDestPath = fso.GetSpecialFolder(mlTEMP_FOLDER) _
& Application.PathSeparator & fso.GetFileName(rsFullPath)
Set tsDest = fso.CreateTextFile(sDestPath, True)
With tsSrc
Do While Not (.AtEndOfStream Or bDone)
sTemp = .ReadLine
lTruncPos = InStr(1, sTemp, rsTruncString, _
vbTextCompare)
If lTruncPos Then
sTemp = Left$(sTemp, lTruncPos - 1)
bDone = True
End If
tsDest.WriteLine sTemp
Loop
End With

tsSrc.Close
tsDest.Close

fso.CopyFile sDestPath, rsFullPath, True

gbTruncateFile = True
End If

ExitRoutine:
On Error Resume Next
tsSrc.Close
tsDest.Close
Kill sDestPath
Set tsSrc = Nothing
Set tsDest = Nothing
Set fso = Nothing
Exit Function
ErrHandler:
Resume ExitRoutine
End Function

-------------------------------------

Sample code if you wanted to read the entire file into a variable:

Sub fdsa()
Dim FileNumber As Integer, FilePath As String
Dim FullString As String
FilePath = "C:\Export.txt"
FileNumber = FreeFile
Open FilePath For Input As #FileNumber
FileLength = LOF(FileNumber)
Do While Not EOF(FileNumber)
Line Input #FileNumber, FullString
MsgBox FullString, vbInformation + vbOKOnly

Loop
Close #FileNumber
End Sub

You should be able to coble something together.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top