![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | find last non-empty line Hi, I have the following situation: I need to read the last line of a plain- text file. But it can contain emtpy lines. So I am looking for a way to check if a line is empty. I could do a top-to-bottom analysis of the file and use ReadLine for every line, but I was wondering whether there is a faster way. Like read the entire file, and then read it from the end somehow. tia arno |
My System Specs![]() |
| | #2 (permalink) |
| | Re: find last non-empty line "arno" <noreplyAThighiqDOTnl@xxxxxx> wrote in message news:MPG.23dddf38da680d6a98968b@xxxxxx Quote: > Hi, > > I have the following situation: I need to read the last line of a plain- > text file. But it can contain emtpy lines. So I am looking for a way to > check if a line is empty. I could do a top-to-bottom analysis of the > file and use ReadLine for every line, but I was wondering whether there > is a faster way. Like read the entire file, and then read it from the > end somehow. last non-blank line. If it is very large you will wind up doing the blank/non-blank test lots of time if you read through line by line. If you read the whole file in at one time (possibly resulting in less time taken up by reading the file) into an array and process this in reverse order, you will likely do fewer comparisons. It is not really possible to determine where the trade off is. On top of it all, consider two huge files containing all blank lines but one: the first line in one file and the last line in the other. BTW, the fastest way to read in an entire text file is to use the .read method, and specify a number of bytes equal to the size of the file. For large files this can be significantly more efficient than .readall, because that buffers the input and performs string concatenation. I'd suggest that you select your algorithm based on its simplicity. If it takes too long, you could then look for ways to tweak it. How you check lines to see if they are empty, well, that depends on what you mean by an empty line. Does it mean a line containing no characters (i.e. a line of length - zero), containing any number of blanks, or containing any number of "whitespace" characters (blank, tab, vertical tab?). /Al |
My System Specs![]() |
| | #3 (permalink) |
| | Re: find last non-empty line "arno" <noreplyAThighiqDOTnl@xxxxxx> wrote in message news:MPG.23dddf38da680d6a98968b@xxxxxx Quote: > Hi, > > I have the following situation: I need to read the last line of a plain- > text file. But it can contain emtpy lines. So I am looking for a way to > check if a line is empty. I could do a top-to-bottom analysis of the > file and use ReadLine for every line, but I was wondering whether there > is a faster way. Like read the entire file, and then read it from the > end somehow. example. Dim oRegEx, oMatches, oFSO, sLastNonEmptyLine, sFile, sText Set oRegEx = CreateObject("VBScript.RegExp") Set oFSO = CreateObject("Scripting.FileSystemObject") sFile = "C:\Test.txt" oRegEx.MultiLine = True oRegEx.Global = True oRegEx.Pattern = "^[^\n\r\b]+$" sText = oFSO.OpenTextFile(sFile, 1).ReadAll Set oMatches = oRegEx.Execute(sText) sLastNonEmptyLine = oMatches(oMatches.Count - 1) MsgBox sLastNonEmptyLine, vbInformation, "Last Non-Empty Line" |
My System Specs![]() |
| | #4 (permalink) |
| | Re: find last non-empty line s = TS.ReadAll A = Split(s, vbCrLf) For i = Ubound(A) to 0 Step -1 s = Trim(A(i)) If len(s) > 0 Then MsgBox "Last line is: " & s Exit For End If Next The above adds an extra Trim operation just to make sure you don't read a line with a space as the last line. You may not need that part. Quote: > I have the following situation: I need to read the last line of a plain- > text file. But it can contain emtpy lines. So I am looking for a way to > check if a line is empty. I could do a top-to-bottom analysis of the > file and use ReadLine for every line, but I was wondering whether there > is a faster way. Like read the entire file, and then read it from the > end somehow. > > tia > arno > |
My System Specs![]() |
| | #5 (permalink) |
| | Re: find last non-empty line Interesting input, tx all arno In article <O6kKrmeeJHA.5748@xxxxxx>, mayaXXyana@xxxxxx says...> Quote: > s = TS.ReadAll > A = Split(s, vbCrLf) > For i = Ubound(A) to 0 Step -1 > s = Trim(A(i)) > If len(s) > 0 Then > MsgBox "Last line is: " & s > Exit For > End If > Next > > The above adds an extra Trim operation just > to make sure you don't read a line with a space > as the last line. You may not need that part. > Quote: > > I have the following situation: I need to read the last line of a plain- > > text file. But it can contain emtpy lines. So I am looking for a way to > > check if a line is empty. I could do a top-to-bottom analysis of the > > file and use ReadLine for every line, but I was wondering whether there > > is a faster way. Like read the entire file, and then read it from the > > end somehow. > > > > tia > > arno > > |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Vista explorer - Folder empty, but is not empty, why? | Vista General | |||
| add an empty line to txt file every two rows. | PowerShell | |||
| file folder icons display non-empty folders as empty | Vista file management | |||
| How to find base address to read cmd line argument from the proces | Vista General | |||
| Mysterious empty line in DateTime formatting | PowerShell | |||