vb script count number of lines in txt

1,050 views
Skip to first unread message

Raju Prasad

unread,
Feb 25, 2012, 8:04:40 AM2/25/12
to mercu...@googlegroups.com, gcr...@googlegroups.com

Hi
testers

using Vb script how can we count number of lines present in a txt file 

thanks
yours
reddy prasad

gokul krishna

unread,
Feb 26, 2012, 12:57:45 AM2/26/12
to mercu...@googlegroups.com, gcr...@googlegroups.com
Set fso= CreateObject("Scripting.FileSystemObject")

Set strtext=fso.OpenTextFile("filepath",1)

text=strtext.ReadAll

atext=Split(text,vbCrLf)

MsgBox"no. of lines"&CStr(UBound(atext)+1)

Thanks,
Gokul.


--
You received this message because you are subscribed to the Google
"QTP - HP Quick Test Professional - Automated Software Testing"
group.
To post to this group, send email to Mercu...@googlegroups.com
To unsubscribe from this group, send email to
MercuryQTP+...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/MercuryQTP?hl=en




Mahattam Tomar

unread,
Feb 25, 2012, 11:41:50 PM2/25/12
to mercu...@googlegroups.com, gcr...@googlegroups.com
Hi Reddy,

You can use split function to split the all content of file with newline, it will give an array and ubound plus one of that array will be total number of line in text file.
please find the code below...

str="C:\sample.txt"
Set fso=createobject("scripting.filesystemobject")
set f=fso.opentextfile(str,1)
arrTemp=split(f.readall,vbNewline)
intTotalLine=ubound(arrTemp)+1
msgbox intTotalLine

Thanks & Regards
Mahattam Singh Tomar

On Sat, Feb 25, 2012 at 6:34 PM, Raju Prasad <raju16...@gmail.com> wrote:

--

Singh

unread,
Feb 25, 2012, 4:46:17 PM2/25/12
to QTP - HP Quick Test Professional - Automated Software Testing
You can try notepad2

tariful islam

unread,
Feb 26, 2012, 1:25:24 AM2/26/12
to mercu...@googlegroups.com
 Hi Raju
 This code might help u......
 
 
Set ofso=createobject("scripting.filesystemobject")
Set otextfile=ofso.OpenTextFile("c:\testing.txt",1)    '1 for reading
otextfile.ReadAll
print "Number of lines in a text file:" &otextfile.Line
otextfile.Close
Set otextfile=nothing
Set ofso=nothing
 
 
 
Thanks & Regards,
Tariful Islam

Raju Prasad

unread,
Feb 26, 2012, 9:07:21 PM2/26/12
to mercu...@googlegroups.com
Hi
tariful

thanks for providing the code

BUT

in my txt file it consists of 9 lines but in output is showing 10 lines
plz go thru the atached doc

thanks
raju

> --
> You received this message because you are subscribed to the Google
> "QTP - HP Quick Test Professional - Automated Software Testing"
> group.
> To post to this group, send email to Mercu...@googlegroups.com
> To unsubscribe from this group, send email to
> MercuryQTP+...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/MercuryQTP?hl=en
>


--
yours
reddy prasad

text_file.txt

santhosh Ganji

unread,
Feb 27, 2012, 12:01:49 AM2/27/12
to mercu...@googlegroups.com
Hi,

Please check the text file once
Its having 10 lines
The code is working fine.
If you click enter after the line completed, that will be treated as a new line even you didn't enter any thing...
Thanks and Reagards
Santhosh G
+918007850250


Raju Prasad

unread,
Feb 27, 2012, 6:11:25 AM2/27/12
to mercu...@googlegroups.com
ok  santhoshkumar i got it 

Mahattam Tomar

unread,
Feb 27, 2012, 10:49:43 AM2/27/12
to mercu...@googlegroups.com
HI Tarif,

Your text file contains 10 lines, there is 9 lines you can see but the last line is null line.
So when you will be running code it will give you 10 line including last line. You need to remove last null line.

Thanks & Regards
Mahattam Singh Tomar
On Mon, Feb 27, 2012 at 7:37 AM, Raju Prasad <raju16...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages