Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

Deleting a line from a text file?

I have three text files each containing information.  I am passing a string to a function which loops through the files.  If the string exists in the file, I want to delete it.

This is the code I have:

for x = 1 to 3
     Open strTextFileName(x) For Input As #x
     Do While Not EOF(x)
        ReDim Preserve strLines(NumLines)
        Line Input #x, strLines(NumLines)
        NumLines = NumLines + 1
     Loop
     Close #x
     Open strTextFileName(x) For Output As #x
     
     For i = 0 To UBound(strLines)
      If UCase(Trim(strLines(i))) = UCase(Trim & _          (strDelete)) Then
      MsgBox strLines(i) & " is the same as " & strDelete
      Else
            Write #x, strLines(i)
      End If
     Next
     Close #x
Next


However, this doesn't delete the line.

Any help on how i could do this would be greatly appreciated.

0
Cbel
Asked:
Cbel
1 Solution
 
Renato102098Commented:
No you do not delete the line in text file.
You may be create new text file. Read each line from source file and then write in new file if the line may stay in new file.

Bye
Renato
0
 
CbelAuthor Commented:
This works!

For x = 1 To 3
        Open strTextFileName(x) For Input As #1
        Do While Not EOF(1)
            ReDim Preserve strLines(NumLines)
            Input #1, strLines(NumLines)
            NumLines = NumLines + 1
        Loop
        Close #1
       
        Kill strTextFileName(x)
        Open strTextFileName(x) For Append As #1
       
        For i = LBound(strLines) To UBound(strLines)
           If strLines(i) <> strDelete Then
              Write #1, strLines(i)
        End If
Next
        Close #1
        ReDim strLines(NumLines)
        Next
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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