A Microsoft Excel forum. ExcelBanter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » ExcelBanter forum » Excel Newsgroups » Excel Programming
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

File truncation with VBA



 
 
Thread Tools Display Modes
  #1  
Old August 23rd 05, 03:58 PM posted to microsoft.public.excel.programming
crazybass2
external usenet poster
 
Posts: 167
Default File truncation with VBA

I have several text files that are very large (500MB+). There is a keyword
in each file ("Array") at which point I don't need the data below and
including the line in appears. Problem is, using a text editor for such a
large file makes it nearly impossible to Find, Select-to-End, Delete.

I currently have an excel file that will input each line into a cell and
until it reaches the keyword. Once the keyword is found, the file is closed
and a new file is opened and all the cells are written to the file. With
files as large as I have go thru this process it takes more than two hours to
finish. I'm sure that the majority of the time is spent writing the data to
the cells and writing it back to a new file.

Is there a way (in Excel) to find a keyword in a text file and delete
everything from that line on?
Ads
  #2  
Old August 23rd 05, 06:05 PM posted to microsoft.public.excel.programming
crazybass2
external usenet poster
 
Posts: 167
Default File truncation with VBA

Jake,

That was almost perfect! I needed for the entire line containting "Array"
to be deleted, not just after "Array". It was a quick mod to your code to
accomplish. Thanks for your quick response!

Much appreciated,
Mike

"Jake Marx" wrote:

> Hi crazybass2,
>
> You can do this pretty easily without bringing the file into Excel. Here's
> a function that should do what you're looking for. Just pass in the full
> path/filename and the string you want to truncate after (in this case,
> "array").
>
> I don't know how quickly this will execute against a 500MB+ file, but it's
> worth a try to see if it's any faster. It should be IMO.
>
>
> 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
>
> --
> Regards,
>
> Jake Marx
> MS MVP - Excel
> www.longhead.com
>
> [please keep replies in the newsgroup - email address unmonitored]
>
>
> crazybass2 wrote:
> > I have several text files that are very large (500MB+). There is a
> > keyword in each file ("Array") at which point I don't need the data
> > below and including the line in appears. Problem is, using a text
> > editor for such a large file makes it nearly impossible to Find,
> > Select-to-End, Delete.
> >
> > I currently have an excel file that will input each line into a cell
> > and until it reaches the keyword. Once the keyword is found, the
> > file is closed and a new file is opened and all the cells are written
> > to the file. With files as large as I have go thru this process it
> > takes more than two hours to finish. I'm sure that the majority of
> > the time is spent writing the data to the cells and writing it back
> > to a new file.
> >
> > Is there a way (in Excel) to find a keyword in a text file and delete
> > everything from that line on?

>
>

  #3  
Old August 23rd 05, 06:05 PM posted to microsoft.public.excel.programming
crazybass2
external usenet poster
 
Posts: 167
Default File truncation with VBA

Oh, and BTW...it took less than 30 seconds to process a 680MB file down to a
78MB file.

Again, Thanks!



"Jake Marx" wrote:

> Hi crazybass2,
>
> You can do this pretty easily without bringing the file into Excel. Here's
> a function that should do what you're looking for. Just pass in the full
> path/filename and the string you want to truncate after (in this case,
> "array").
>
> I don't know how quickly this will execute against a 500MB+ file, but it's
> worth a try to see if it's any faster. It should be IMO.
>
>
> 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
>
> --
> Regards,
>
> Jake Marx
> MS MVP - Excel
> www.longhead.com
>
> [please keep replies in the newsgroup - email address unmonitored]
>
>
> crazybass2 wrote:
> > I have several text files that are very large (500MB+). There is a
> > keyword in each file ("Array") at which point I don't need the data
> > below and including the line in appears. Problem is, using a text
> > editor for such a large file makes it nearly impossible to Find,
> > Select-to-End, Delete.
> >
> > I currently have an excel file that will input each line into a cell
> > and until it reaches the keyword. Once the keyword is found, the
> > file is closed and a new file is opened and all the cells are written
> > to the file. With files as large as I have go thru this process it
> > takes more than two hours to finish. I'm sure that the majority of
> > the time is spent writing the data to the cells and writing it back
> > to a new file.
> >
> > Is there a way (in Excel) to find a keyword in a text file and delete
> > everything from that line on?

>
>

  #4  
Old August 23rd 05, 06:28 PM posted to microsoft.public.excel.programming
Jake Marx[_3_]
external usenet poster
 
Posts: 860
Default File truncation with VBA

No problem - glad it helped!

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]


crazybass2 wrote:
> Oh, and BTW...it took less than 30 seconds to process a 680MB file
> down to a 78MB file.
>
> Again, Thanks!
>
>
>
> "Jake Marx" wrote:
>
>> Hi crazybass2,
>>
>> You can do this pretty easily without bringing the file into Excel.
>> Here's a function that should do what you're looking for. Just pass
>> in the full path/filename and the string you want to truncate after
>> (in this case, "array").
>>
>> I don't know how quickly this will execute against a 500MB+ file,
>> but it's worth a try to see if it's any faster. It should be IMO.
>>
>>
>> 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
>>
>> --
>> Regards,
>>
>> Jake Marx
>> MS MVP - Excel
>> www.longhead.com
>>
>> [please keep replies in the newsgroup - email address unmonitored]
>>
>>
>> crazybass2 wrote:
>>> I have several text files that are very large (500MB+). There is a
>>> keyword in each file ("Array") at which point I don't need the data
>>> below and including the line in appears. Problem is, using a text
>>> editor for such a large file makes it nearly impossible to Find,
>>> Select-to-End, Delete.
>>>
>>> I currently have an excel file that will input each line into a cell
>>> and until it reaches the keyword. Once the keyword is found, the
>>> file is closed and a new file is opened and all the cells are
>>> written to the file. With files as large as I have go thru this
>>> process it takes more than two hours to finish. I'm sure that the
>>> majority of the time is spent writing the data to the cells and
>>> writing it back to a new file.
>>>
>>> Is there a way (in Excel) to find a keyword in a text file and
>>> delete everything from that line on?

 




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
excel 2003 imported csv file, truncation after decimal point RML Excel Discussion (Misc queries) 2 February 23rd 09 11:34 PM
Print truncation sbweid Excel Discussion (Misc queries) 0 November 8th 05 04:25 PM
truncation problem mbk141[_2_] Excel Programming 0 October 27th 04 02:51 PM
truncation problem mbk141 Excel Programming 2 October 27th 04 03:39 AM
string truncation ljorn Excel Programming 0 September 11th 03 02:23 PM


All times are GMT +1. The time now is 11:49 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Copyright ©2004-2013 ExcelBanter.
The comments are property of their posters.