Search This Blog

Thursday 27 October 2011

Text Files/Flat Files/Sequential Files Examples


EX:-4
                        Write vbscript QTP, to display no:of words in every line of file.

                                    Option explicit
                                    Dim fso,fo,l,nw,p
                                    Set fso=createobject(“scripting.filesystemobject”)
                                    Set fo=fso.opentextfile(“c:/sample.txt”,1,false)
                                    While fo.atendofstream< >true
                                                l=fo.readline
                                                p=split(l,” “)
                                                nw=ubound(p)+1
                                    wend
                                    print(“no:of words in file”&nw)
                                    fo.close
                                    set fo=nothing
                                    set fso=nothing


            Ex:-5
                        To display only numeric letters in the given file.

                                    Option explicit
                                    Dim fso,fo,l,p,ps
                                    Set fso=createobject(“scripting.filesystemobject”)
                                    Set fo=fso.opentextfile(“c:\sample.txt”,1,false)
                                    While fo.atendofstream< >true
                                                l=fo.readline
                                                ps=split(l,” “)
                                                for each p in ps
                                                            if isnumeric(p) then
                                                                        print(p)
                                                            end if
                                                next
                                    wend
                                    fo.close
                                    set fo=nothing
                                    set fso=nothing

            EX:-6
                        To get inbuilt integer numbers in every line of file.

                                    Option explicit
                                    Dim fso,fo,l,nums,num
                                    Set fso=createobject(“scripting.filesystemobject”)
                                    Set fo=fso.opentextfile(“c:\sample.txt”,1,false)
                                    While fo.atendofstream< >true
                                                l=fo.readline
                                                set r=new regexp
                                                r.pattern=”[0-9]+”
                                                r.global=true
                                                set nums=r.execute(l)
                                                for each num in nums
                                                            print(num.value)
                                                next
                                                set nums=nothing
                                                set r=nothing
                                    wend
                                    fo.close
                                    set fo=nothing
                                    set fso=nothing
                                   

EX:-7
            To get only amount in below file.

                        Data.txt
                       
                        Sunitha salary is $202.12
                        $789 amount is salary of  kishore
                        Kiran sal is $222.33

                        Option explicit
                        Dim fso,fo,l,nums,num
                        Set fso=createobject(“scripting.filesystemobject”)
                        Set fo=fso.opentextfile(“c:\data.txt”,1,false)
                        While fo.atendofstream< >true
                                    l=fo.readline
                                    set r=new regexp
                                    r.pattern=”[$][0-9]+[\.]?[0-9]{2}”
                                    r.global=true
                                    set nums=r.execute(l)
                                    for each num in nums
                                                print(num.value)
                                    next
                                    set nums=nothing
                                    set r=nothing
                        wend
                        fo.close
                        set fo=nothing
                        set fso=nothing

No comments:

Post a Comment