| Requires reference to Microsoft Scripting Runtime. See comments within code for explanation/usage Public Function DeleteLine(fName As String, LineNumber As Long) As Boolean 'Purpose: Deletes a Line from a text file 'Parameters: fName = FullPath to File ' LineNumber = LineToDelete 'Returns: True if Successful, false otherwise 'Requires: Reference to Microsoft Scripting Runtime 'Example: DeleteLine("C:\Myfile.txt", 3) 'Deletes third line of Myfile.txt '______________________________________________________________ Dim oFSO As New FileSystemObject Dim oFSTR As Scripting.TextStream Dim ret As Long Dim lCtr As Long Dim sTemp As String, sLine As String Dim bLineFound As Boolean On Error GoTo ErrorHandler If oFSO.FileExists(fName) Then Set oFSTR = oFSO.OpenTextFile(fName) lCtr = 1 Do While Not oFSTR.AtEndOfStream sLine = oFSTR.ReadLine If lCtr <> LineNumber Then sTemp = sTemp & sLine & vbCrLf Else bLineFound = True End If lCtr = lCtr + 1 Loop oFSTR.Close Set oFSTR = oFSO.CreateTextFile(fName, True) oFSTR.Write sTemp DeleteLine = bLineFound End If ErrorHandler: On Error Resume Next oFSTR.Close Set oFSTR = Nothing Set oFSO = Nothing End Function |
Delete a Specified Line from a Text File |
Express News India | Freelance ecommerce web development India