Hi
Using FSO i would like to determine the LineNumber of Text File if the Following examples as per 3 cases
The below cases demonstrates the line nos and are the Last Lines of Text File
The line Nos can be viewed in Notepad ++
As per Case 2 I would like to have Msg Displayed as Total Lines :1787 The Last And Non Empty Line is Line no. 1787
As per Case 3 I would like to have Msg Displayed as Total Lines :1794 The Last Non Empty Line is Line no. 1787 and The Last Blank Line No. is 1794
Below is the code for above result only was able to achieve the result of Case 2. FYI
Your help will be indeed appreicated
Thanks and Regard
NimishK
Using FSO i would like to determine the LineNumber of Text File if the Following examples as per 3 cases
The below cases demonstrates the line nos and are the Last Lines of Text File
The line Nos can be viewed in Notepad ++
As per Case 1 I would like to have Msg Displayed as Total Lines :1788 The Last Non Empty Line is Line no. 1787 and The Last Blank Line No. is 1788Case 1
1784
1785 The Brown Fox
1786 Quickly Jumped over
1787 The Lazy Dogs
1788 # This is the Cursor Blinking on the Last Blank Line 1788
1789
....
....
Case 2
1784
1785 The Brown Fox
1786 Quickly Jumped over
1787 The Lazy Dogs# This is the Cursor Blinking on the Last Non-Empty/Blank Line 1787
1788
1789
....
....
Case 3
1784
1785 The Brown Fox
1786 Quickly Jumped over
1787 The Lazy Dogs
1788
1789
1790
1791
1792
1793
1794# This is the Cursor Blinking on the Last Blank Line 1794
....
....
As per Case 2 I would like to have Msg Displayed as Total Lines :1787 The Last And Non Empty Line is Line no. 1787
As per Case 3 I would like to have Msg Displayed as Total Lines :1794 The Last Non Empty Line is Line no. 1787 and The Last Blank Line No. is 1794
Below is the code for above result only was able to achieve the result of Case 2. FYI
VBA Code:
Private Sub FsoMethod()
Dim objfso As Object, ObjFile As Object, lCount As Long
Set objfso = CreateObject("Scripting.FileSystemObject")
Dim FileName As String
Dim strAllData As String
Dim StrLines() As String
Dim ArrFileLines() As Variant
Dim LstLineNo As Long
Dim FileNum As Integer
Dim LastNonBlankLineNo As Long
Dim i As Long, ln As Integer
Dim StrLinesData As String
Dim strLastLine As String, strline As String, strLinenumber As Long, totLines As Long
Dim LastEmptyLineNo As Integer
Dim msgEmpty As String, msgNonEmpty As String
FileName = "C:\ABC\Trial.txt"
FileNum = FreeFile
LstLineNo = 1
Const ForReading = 1
Set ObjFile = objfso.OpenTextFile(FileName, ForReading)
i = 0
Do While Not ObjFile.AtEndOfStream
strLastLine = ObjFile.ReadLine
totLines = ObjFile.Line
If Len(strLastLine) > 0 Then
StrLinesData = strLastLine
LastEmptyLineNo = ObjFile.Line
End If
Loop
If totLines = LastEmptyLineNo Then
msgEmpty = "The Last And Non Empty Line is Line no."
strAllData = "TOTAL LINES : " & totLines & " " & msgEmpty & LastEmptyLineNo
End If
If totLines > LastEmptyLineNo Then
LastEmptyLineNo = totLines - LastEmptyLineNo
msgEmpty = " The Last Non Empty Line is Line no. "
strAllData = "TOTAL LINES : " & totLines & " " & StrLinesData & msgEmpty & LastEmptyLineNo & " The last Blank Line is "
End If
TextBox2.Text = strAllData
End Sub
Thanks and Regard
NimishK
Last edited: