Solved

How can I do: Delete a line in a file!

Posted on 2002-03-08
8
128 Views
Last Modified: 2010-05-02
I wanna delete a line in a file. How can I do this?
0
Comment
Question by:ogir
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 2

Expert Comment

by:mmcmillen
Comment Utility
What kind of file?
0
 
LVL 2

Expert Comment

by:DanielBlais
Comment Utility
you must copy all line before and all line after the line you want to delete in a temporary file and then copy this file over the first one.
0
 
LVL 18

Expert Comment

by:bobbit31
Comment Utility
simple example:

Open "C:\my documents\test1.txt" For Input As #1
Open "C:\my documents\test2.txt" For Output As #2

Dim lineNum As Integer
lineNum = 0

While Not EOF(1)
    '' in case you want to delete by line number
    lineNum = lineNum + 1
    Input #1, strLine
   
    '' just an example you could also see if strLine = the line you want to delete
    If lineNum <> 3 Then
        Print #2, strLine
    End If
   
Wend

Close (1)
Close (2)
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
Alternatives:
1. use and Input() function and read all the contents of the file into a string variable.  Remove the line by moving the later part of the file contents to the start of the first line (string concatenation with Left$() and Mid$() functions).  Print the modified string back to the file.  Remember that a line includes the trailing vbCrLf characters.

1a. similar to (1) but you use two Print statements to output the file...all lines before the deleted line and all lines after the deleted line.  Use a semicolon on the first Print statement to prevent an extra vbCrLf between the two file parts.

1b. read the lines with Line Input statement and concatenate the contents & vbCrLf to a string variable.  Print the entire contents back out in one operation.

2. if the lines are the same length, open the file as Random and delete the line.

3. automate Word to open the file, delete the line, and save the file.

4. read the entire contents into an RTF control, remove the line, and save the contents.  The RTF control has methods for opening a file and saving to a file.

=================================
Advantages and Notes:
(1) does all its I/O in two statements, but your file can't be overly large (or your PC will start using virtual memory).
(1) is very fast
(1) writes back over the original file.  bobbit31 solution creates a second file with the line omitted.
(1a) for large files, this is quicker than (1), since there are performance problems with large string manipulation (concatenation).
(1b) might make it easier to identify your line, since it reads the file one line at a time.
(2) takes a database approach to the problem, but is limited in its application.

=================================
Question: Do you want to remove the line entirely or just remove the line's contents, leaving the vbCrLf?
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:ogir
Comment Utility
I have a sequential file, and I dont wanna use 2 files. What I wanna know is: "Have some command that delete/eliminate all the line I want?". For example:

My actual file content.
aaaaaaaaaaaaaaaaaaaaaaaa - line 1
bbbbbbbbbbbbbbbbbbbbbbbb - line 2
cccccccccccccccccccccccc - line 3

My desired file content after the command.
aaaaaaaaaaaaaaaaaaaaaaaa - line 1
cccccccccccccccccccccccc - line 2
0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 100 total points
Comment Utility
another example that does what you want (based on option 1b of aikimark's post)

Private Sub deleteLine(line As Integer)

Dim strLine As String
Dim strFile As String

Open "C:\my documents\test2.txt" For Input As #1

Dim lineNum As Integer
lineNum = 0

While Not EOF(1)
   '' in case you want to delete by line number
   lineNum = lineNum + 1
   Line Input #1, strLine
   
   '' just an example you could also see if strLine = the line you want to delete
   If lineNum <> line Then
       strFile = strFile & strLine & vbCrLf
   End If
   
Wend

Close (1)

Open "C:\my documents\test2.txt" For Output As #1
Print #1, strFile

Close (1)

End Sub

Private Sub Command1_Click()

    deleteLine 3

End Sub
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
Then use any of 1,1a, 1b, 3, or 4.

1b example:
intFilenum = Freefile
open "your path and filename" for Binary as #intFilenum
do until eof(#intFilenum)
  line input #intFilenum, strTemp
  if strTemp = "bbbbb" then  'I'm not sure of your criteria
  else
     strCleaned = strCleaned & strTemp & vbCrLf
  end if
Loop
Close #intFilenum
open "your path and filename" for output as #intFilenum
Print #intFilenum, strCleaned ;
Close #intFilenum

=======================================
Performance Note 1:
If you only need to delete one line, you can use the Input() function to read the rest of the file once you've encountered the line to be deleted.  use Exit Do to terminate the line-by-line reading.

Performance Note 2:
If you need to delete the line based on its ordinal position in the file (i.e. second, third, etc.), not based on the contents, you can use a For...Next loop to read the lines before and the Input() function to read the lines after the line to be deleted.
0
 

Author Comment

by:ogir
Comment Utility
Thank all you.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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…
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…
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…

762 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

6 Experts available now in Live!

Get 1:1 Help Now