PC Review


Reply
Thread Tools Rate Thread

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

 
 
=?Utf-8?B?SkBZ?=
Guest
Posts: n/a
 
      27th Jun 2007
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
 
Reply With Quote
 
 
 
 
=?Utf-8?B?VG9tIE9naWx2eQ==?=
Guest
Posts: n/a
 
      27th Jun 2007
http://msdn.microsoft.com/library/en...ce09072000.asp
Working with Files, Folders and Drives: More VBA Tips and Tricks by David
Shank

http://support.microsoft.com/default...b;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.

--
Regards,
Tom Ogilvy


"J@Y" wrote:

> 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

 
Reply With Quote
 
=?Utf-8?B?SkBZ?=
Guest
Posts: n/a
 
      27th Jun 2007
Great thanks! I was looking for specific tutorials like these

"Tom Ogilvy" wrote:

> http://msdn.microsoft.com/library/en...ce09072000.asp
> Working with Files, Folders and Drives: More VBA Tips and Tricks by David
> Shank
>
> http://support.microsoft.com/default...b;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.
>
> --
> Regards,
> Tom Ogilvy
>
>
> "J@Y" wrote:
>
> > 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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
searching code and queries for text Mark Andrews Microsoft Access 5 18th Dec 2009 05:35 PM
searching and copying text in drawing layer of excel? =?Utf-8?B?amVmZmVyeQ==?= Microsoft Excel Misc 3 10th Nov 2006 04:41 PM
Searching Text in PDF file Twinkle Microsoft C# .NET 1 14th Sep 2006 05:49 PM
Searching multiple files and copying text Marcelo P. Microsoft Excel Programming 0 23rd Jul 2004 08:30 PM
VB searching in text file AL Microsoft VB .NET 17 5th Sep 2003 09:52 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:22 AM.