Solved

How can I delete first 15 lines in a text file

Posted on 1999-01-11
8
174 Views
Last Modified: 2010-05-03
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.
0
Comment
Question by:lesa
  • 5
  • 2
8 Comments
 
LVL 2

Accepted Solution

by:
VBDesigns earned 10 total points
ID: 1455543
If it's a text file, this way should work:

Dim iFF as Integer
Dim lLength as Long
CONST MAX_FILE_LEN = 15000
Dim sValue as String * MAX_FILE_LEN

iFF = FreeFile

Open "c:\textfile.txt" for binary as #iFF
lLength = LOG (#iFF)
if lLength > MAX_FILE_LEN then
   Seek(#iFF), (lLength - MAX_FILE_LEN)
   Get #iFF, , sValue
endif

Close #iFF

Kill "c:\textfile.txt"

Open "c:\textfile.txt" for Binaryt as #iFF
Put #iFF, , sValue
Close # iFF
0
 

Author Comment

by:lesa
ID: 1455544
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.
0
 

Author Comment

by:lesa
ID: 1455545
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.
0
 

Author Comment

by:lesa
ID: 1455546
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:lesa
ID: 1455547
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.
0
 

Author Comment

by:lesa
ID: 1455548
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.
0
 
LVL 3

Expert Comment

by:vikiing
ID: 1455549
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.


0
 
LVL 3

Expert Comment

by:vikiing
ID: 1455550
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)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now