Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Deleting lines in a file?

Posted on 1999-01-20
5
165 Views
Last Modified: 2010-05-03
how can i open a file in VB and delete the first 3 lines?
is there a deletelines function or something like that?
0
Comment
Question by:sheets1
  • 2
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
jjbyers earned 20 total points
ID: 1469660
I'm assumming a Text file.

open "Filename" for input as #1
open "Newfilename" for output as #2
cnt=0
do until eof(1)
  line input #1,a$
  if cnt>2 then
    print #2,a$
  end if
  cnt=cnt+1
loop
close #1
close #2
0
 
LVL 3

Expert Comment

by:vikiing
ID: 1469661
There's no direct process which can delete lines at top (or middle) of a sequential file. The only thing you can do is "to copy" the file into another, discarding the first lines as Jjbyers told you.

0
 

Author Comment

by:sheets1
ID: 1469662
Thanks to the both of you BUT i have another problem in that i want to delete the very last line in the file. how could i do this?
0
 
LVL 3

Expert Comment

by:jjbyers
ID: 1469663
You could read the lines into an array and then only output to another file the lines you want.
0
 
LVL 3

Expert Comment

by:vikiing
ID: 1469664
To truncate the last line, you can do this:

Dim B as byte : open <your file> for binary as 1 : s&=lof(1)
count%=0

do     'Read the file backwards until find the 2nd <LF>
    s&=s&-1 : seek #1,s& : Get #1,1,B
    if b=10 then count%=count%+1 : if count%=2 then exit do
loop
a$="" : put #1,,a$ : close #1

After completion of that, the file is truncated at the point where a <LF> byte is found, getting rid of the last line.

Watch out: this assumes file has AT LEAST two lines.


0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

840 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