CeciliA
CeciliA

Reputation: 45

Removing Blank Space after Deleting a line in Text File VB6

i have a problem right now in deleting line in a text file, After i delete a line of string in textfile, there a blank space retaining on it. Here`s my code for it. Thanks for helping me Guys.

iFileList = FreeFile
iFileList2 = FreeFile

Open App.Path & "\months\" & gMonth & ".txt" For Input As #iFileList

Do While Not EOF(iFileList)
    Line Input #iFileList, sLine
    tempHolder2 = Split(sLine, "/")
    If Len(sLine) > 0 Then
        If gDay = tempHolder2(0) Then
            If tempHolder2(1) Like lvAlarm.selectedItem Then
                'skip the line
            Else
                sNewText = sNewText & sLine & vbCrLf
            End If
        End If
    End If

Loop
Close
Debug.Print (sNewText)
iFile = FreeFile
Open App.Path & "\months\" & gMonth & ".txt" For Output As #iFile
'sNewText = sNewText & vbCrLf
Print #iFile, Trim(sNewText)
Close

Upvotes: 0

Views: 468

Answers (1)

Bob Mc
Bob Mc

Reputation: 2008

I'm guessing that the blank space that you're referring to is at the end of the file, not somewhere interspersed within the other lines. If that is the case, I suspect it's because the Trim command that you use when printing the contents of the sNewText variable to the file does not remove the last carriage return/line feed pair at the end of the string.

To remove the trailing line break, you should probably do the following:

If Right$(sNewText,2) = vbCrLf Then
    sNewText = Left$(sNewText, Len(sNewText) - 2)
End If

Upvotes: 2

Related Questions