[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Deleting lines in a file?

Posted on 1999-01-20
5
Medium Priority
?
169 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
[X]
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
  • 2
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
jjbyers earned 40 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Suggested Courses

650 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