Avatar of axnst2
axnst2Flag for United States of America

asked on 

deleting a line from a .TXT

I need to delete a specific line from a .TXT file.

I am reading through the file and if I find the line that I am looking for, then I need to delete it.

I know ways to get rid of it, but is there an easy way of doing this?

Thanks,
axnst2
Visual Basic Classic

Avatar of undefined
Last Comment
Glowman
Avatar of vb_elmar
vb_elmar
Flag of Germany image

Try this:

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Form_Activate()
Move 0, 15 * 600, 15 * 120, 15 * 80: DoEvents
Shell "notepad c:\deleteme.txt", vbMaximizedFocus
Sleep 1500
DoEvents
Me.SetFocus

F = InputBox("Enter a line number")
Me.Hide
DoEvents

For x = 1 To F - 1
SendKeys "{down}", True
Next

SendKeys "+{END}", True
SendKeys "%B", True
SendKeys "k", True
z = Clipboard.GetText

Sleep 1400
Me.Show
MsgBox " In the line " & F & Space(60) & _
"i've found the following Data :" & vbCrLf & vbCrLf & z
MsgBox "I copied the data to the Windows Clipboard ."
End
End Sub
Avatar of Glowman
Glowman
Flag of United States of America image

ax,
depending on how big the file is you could try opening it with the FSO(filesystem object) and reading the entire contents into a buffer or temp variable and then issuing a replace() function on it to remove your line and replace with nothing.  Or if it was too big to open in one shot you can use the FSO to go through line by line and if the line you read isn't the line you want to remove readd it to another text file and then rename.  I can come up with real code if you need it not just theorys.
G
Avatar of vb_elmar
vb_elmar
Flag of Germany image

Here is basic code to open a file and read it line by line:

Private Sub readFileLineByLine()
    Dim fileName As String
    Dim inputLine As String
   
    fileName = "c:\someFile.txt"
    If Dir(fileName) <> "" Then
        Open fileName For Input As #1
        While Not EOF(1)
            Line Input #1, inputLine
           
            ' do something with inputLine here....
           
        Wend
        Close #1
    Else
        MsgBox fileName, vbCritical, "File Not Found"
    End If
End Sub
Avatar of vinnyd79
vinnyd79

You could also read the line's into an array,skipping the lines to delete.Then write the array back to the file:


Private Sub Command1_Click()
Dim ff As Integer, Ln As String, arrLn() As String, cnt As Integer

cnt = 0
ff = FreeFile
Open "C:\Testfile.txt" For Input As #ff
Do Until EOF(ff)
Line Input #ff, Ln
If InStr(Ln, "Thing to check for on line") > 0 Then
    ' found so don't add line
Else
    cnt = cnt + 1
    ReDim Preserve arrLn(cnt)
    arrLn(cnt) = Ln
End If

Loop
Close #ff

Dim i As Integer
ff = FreeFile
Open "C:\Testfile.txt" For Output As #ff
For i = 1 To UBound(arrLn)
    Print #ff, arrLn(i)
Next i
Close #ff

End Sub
Avatar of axnst2
axnst2
Flag of United States of America image

ASKER

Thanks glowman, that is exactly what I need.  Do you have any semple code by any chance?

Thanks to the rest of you guys as well!
Avatar of axnst2
axnst2
Flag of United States of America image

ASKER

Also, I will only have about 10 lines in the text file so I please give me a buffer example.

Thanks glowman
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

You like my code do you vb_elmar?   ;)
https://www.experts-exchange.com/questions/21231102/File-I-O.html

Here is code to delete a specific line in a file:

Option Explicit

Private Sub Command1_Click()
    replaceInFile "C:\someFile.txt", "This is a line to delete." & vbCrLf, vbNullString
End Sub

Private Sub replaceInFile(ByVal fileName As String, ByVal oldString As String, ByVal newString As String)
    Dim ff As Integer
    Dim entireFile As String
   
    If Dir(fileName) <> "" Then
        ff = FreeFile
        Open fileName For Binary Access Read As #ff
        entireFile = Input(LOF(ff), ff)
        Close #ff
   
        entireFile = Replace(entireFile, oldString, newString)
   
        ff = FreeFile
        Open fileName For Output As #ff
        Print #ff, entireFile
        Close #ff
    Else
        MsgBox fileName, vbCritical, "File not found"
    End If
End Sub
ASKER CERTIFIED SOLUTION
Avatar of Glowman
Glowman
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Visual Basic Classic
Visual Basic Classic

Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.

165K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo