View Single Post
  #11   Report Post  
Posted to microsoft.public.excel.programming
Auric__ Auric__ is offline
external usenet poster
 
Posts: 538
Default Delete all, but the last 23 lines of an external text file

GS wrote:

Uh.., couple of things worth mentioning here.<g

<1st
I'm not sure why you use the type symbol everywhere the var is used:

Since
Dim n&, fnum&

is the same as

Dim n As Long, fnum As Long

we wouldn't write...

For n As Long =...
or
vText(n As Long)
</1st


Because I've used BASICs where n& and n are *not* the same variable. (I can't
think of which ones offhand, sorry.) I try to be consistent: everything that
uses a type symbol *anywhere*, uses it *everywhere*. (Of course, I don't
*usually* use type symbols on variables, preferring to Dim As type. And of
course, I missed the $ at the declaration of sFile, or else I would've
changed all occurrences to sFile$.)

<2nd
I had occasion to strip trailing blank lines from 1000s of CNC program
files and I did same way using Filter(), only starting with UBound
using Step -1 until I got an element with Len(). CNC prog files are
never empty, but every time a controller writes back edited files the
default line feed was added. That's why my write sub prepends a vbCrLf
to the output string if AppendMode. I never thought to use Redim
Preserve as you do here. I'm definitely going to review that old code
now, after reading your approach!<g
</2nd


One thing I noticed in your code that I wouldn't do myself is this:

For n& = 0 To UBound(vText) - maxLines
vText(n&) = "~"
Next 'n
vText = Filter(vText, "~", False)
WriteTextFileContents Join(vText, vbCrLf), sFile

ISTM that going through the array, changing everything except what you need,
is a waste of CPU cycles (especially with strings). Instead, I would've just
pulled out the elements I actually needed and ignored the rest:

Open outputfile$ For Output As fnum&
For n& = (UBound(vText) - (maxLines - 1)) To UBound(vText)
Print #funm&, vText(n&)
Next
Close fnum&

This could be even moved to a subroutine, something like this:

Sub array1DSliceToFile(filepath As String, arr As Variant, _
startEl As Variant, endEl As Variant)
Dim fnum As Long, L0 As Variant
'some input sanitation...
If VarType(arr) = vbArray Then
If VarType(startEl) = 0 Then startEl = LBound(arr)
If VarType(endEl) = 0 Then endEl = UBound(arr)
If startEl endEl Then Exit Sub
If startEl UBound(arr) Then Exit Sub
If startEl < LBound(arr) Then startEl = LBound(arr)
If endEl < LBound(arr) Then Exit Sub
If endEl UBound(arr) Then endEl = UBound(arr)
End If
If Len(filepath) < 1 Then Exit Sub
'end sanitation
fnum = FreeFile
Open filepath For Output As fnum
If VarType(arr) < vbArray Then
Print #fnum, arr
Else
For L0 = startEl To endEl
Print #fnum, arr(L0)
Next
End If
Close fnum
End Sub

(I can't believe I just spent 45 minutes on this.)

....called like so...

array1DSliceToFile sFile$, vText, (UBound(vText) - maxLines) + 1, _
UBound(vText)

--
Let me not seem to have lived in vain.
-- Tycho Brahe