Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - find last non-empty line

Reply
 
Old 01-18-2009   #1 (permalink)
arno


 
 

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 SpecsSystem Spec
Old 01-18-2009   #2 (permalink)
Al Dunbar


 
 

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.
One way or another you pretty much have to read the entire file to get the
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 SpecsSystem Spec
Old 01-18-2009   #3 (permalink)
James Whitlow


 
 

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.
You can use the 'VBScript.RegExp' object to do this. See below for an
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 SpecsSystem Spec
Old 01-18-2009   #4 (permalink)
mayayana


 
 

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 SpecsSystem Spec
Old 01-19-2009   #5 (permalink)
arno


 
 

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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46