[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 136
  • Last Modified:

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

I wanna delete a line in a file. How can I do this?
0
ogir
Asked:
ogir
  • 2
  • 2
  • 2
  • +2
1 Solution
 
mmcmillenCommented:
What kind of file?
0
 
DanielBlaisCommented:
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
 
bobbit31Commented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
aikimarkCommented:
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
 
ogirAuthor Commented:
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
 
bobbit31Commented:
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
 
aikimarkCommented:
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
 
ogirAuthor Commented:
Thank all you.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now