?
Solved

Deleting a line from a text file?

Posted on 2003-02-24
2
Medium Priority
?
127 Views
Last Modified: 2010-05-01
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
Comment
Question by:Cbel
[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 Comments
 
LVL 1

Accepted Solution

by:
Renato102098 earned 100 total points
ID: 8008998
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
 

Author Comment

by:Cbel
ID: 8024203
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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month10 days, 1 hour left to enroll

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