Link to home
Start Free TrialLog in
Avatar of lesa
lesa

asked on

How can I delete first 15 lines in a text file

I am using file to write log details. I want the size of the file to be less than 15k. So when ever it exceeds 15k i want to delete the oldest data. How can I delete the old data leaving the latest data. I am writing to the file with append mode.
ASKER CERTIFIED SOLUTION
Avatar of VBDesigns
VBDesigns

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lesa
lesa

ASKER

I want the oldest data to be deleted means the top data in the file because as I am using the append method latest will be at bottom. So plese let me know how can I do it.
Avatar of lesa

ASKER

I want the oldest data to be deleted means the top data in the file because as I am using the append method latest will be at bottom. So plese let me know how can I do it.
Avatar of lesa

ASKER

I want the oldest data to be deleted means the top data in the file because as I am using the append method latest will be at bottom. So plese let me know how can I do it.
Avatar of lesa

ASKER

I want the oldest data to be deleted means the top data in the file because as I am using the append method latest will be at bottom. So plese let me know how can I do it.
Avatar of lesa

ASKER

I want the oldest data to be deleted means the top data in the file because as I am using the append method latest will be at bottom. So plese let me know how can I do it.
As the file you're working with is a sequential (text) one, the only thing you can do is:
a) Load file lines into an array (a vector), and calculate how many lines it has
b) Destroy original file and re-create a newer one, starting with line #n (where "n" depends on the amount of data you want to get rid of).

Code can be something like this:

Dim TextLines$(30000) : F%=FreeFile
Open <file> for input as F% : LC%=0
Do Until EOF(F%)
   LC%=LC%+1 : Line Input #F%,TextLines$(LC%)
Loop

Close #F% : Open <file> for output as #F%
For I%=N% to LC%   <==== Read below ====>
   Print #F%,TextLines$(I%)
Next I%

The value N% at For instruction, specifies which is the first line you want to keep in the file (toghether from there downwards). Doing so, you're chopping the first N%-1 lines.

Perhaps you want to chop the file in order its final size be EXACTLY 15 Kb. In that case, value N% must be found counting lengths of lines, this way:

FullSize%=LOF(F%) : N%=0
Do
   N%=N%+1 : FullSize%=FullSize%-Len(TextLines$(N%))-2
Loop Until FullSize%<=15360   '15360 = 15 * 1024 = 15 Kb

When FullSize% reaches 15K, N% will have the first line from which the rest of the file will fit into that size.


I had a mess with "15k" and "15 lines". In the code I've posted, write out the For as

     For I%=16 to LC%

and forget the portion below (because N%=16, and doesn't need to be calculated)