FILE SYSTEM OBJECTS EXAMPLES Examples: i) Create a Folder Dim objFso Set objFso=CreateObject(“scripting.FileSystemObject”) objFso.CreateFolder “C:Documents and Settings1 Desktophyderabad” ii) Check if the Folder Exist or not? If not create the Folder Dim objFso, myFolder myFolder=”C:Documents and Settings1 Desktophyderabad” Set objFso=CreateObject(“scripting.FileSystemObject”) If Not objFso.FolderExists(myFolder) Then objFso.CreateFolder (myFolder) End If iii) Copy a Folder Dim objFso, myFolder myFolder=”C:Documents and Settings1 Desktophyderabad” Set objFso=CreateObject(“scripting.FileSystemObject”) objFso.CopyFolder myFolder,”E:abcd” iv) Delete a folder Dim objFso, myFolder myFolder=”C:Documents and Settings1Desktophyderabad” Set objFso=CreateObject(“scripting.FileSystemObject”)
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
FILE SYSTEM OBJECTS EXAMPLES
Examples:
i) Create a Folder
Dim objFso
Set objFso=CreateObject(“scripting.FileSystemObject”)
objFso.CreateFolder “C:Documents and Settings1 Desktophyderabad”
ii) Check if the Folder Exist or not? If not create the Folder
Dim objFso, myFolder
myFolder=”C:Documents and Settings1 Desktophyderabad”
Set objFso=CreateObject(“scripting.FileSystemObject”)
If Not objFso.FolderExists(myFolder) Then
objFso.CreateFolder (myFolder)
End If
iii) Copy a Folder
Dim objFso, myFolder
myFolder=”C:Documents and Settings1 Desktophyderabad”
Set objFso=CreateObject(“scripting.FileSystemObject”)
objFso.CopyFolder myFolder,”E:abcd”
iv) Delete a folder
Dim objFso, myFolder
myFolder=”C:Documents and Settings1Desktophyderabad”
Set objFso=CreateObject(“scripting.FileSystemObject”)
objFso.DeleteFolder( myFolder)
2nd
Dim objFso, myFolder
myFolder=”C:Documents and Settings1Desktophyderabad”
Set objFso=CreateObject(“scripting.FileSystemObject”)
If objFso.FolderExists(myFolder) Then
objFso.DeleteFolder( myFolder)
End If
v) Return a Collection of Disk Drives
Dim objFso, colDrives
Set objFso=CreateObject(“scripting.FileSystemObject”)
Set colDrives=objFso.Drives
For Each oDrive in colDrives
Msgbox oDrive
Next
vi) Get available space on a Drive
Dim objFso
Set objFso=CreateObject(“scripting.FileSystemObject”)
Set myDrive=objFso.GetDrive(“D:”)
Msgbox myDrive.AvailableSpace/(1024^3) & ” GB”
vii) Creating a Text File
Dim objFso
Set objFso=CreateObject(“scripting.FileSystemObject”)
objFso.CreateTextFile (“C:Documents and Settings1 Desktopabcd.txt”)
objFso.CreateTextFile (“C:Documents and Settings1 Desktopabcd.doc”)
objFso.CreateTextFile (“C:Documents and Settings1 Desktopabcd.xls”)
objFso.CreateTextFile (“C:Documents and Settings1 Desktopabcd.pdf”)
Note: We can Create other files also, but they act as Text/Flat Files
viii) Check if the File Exist or not? If not create the File
Dim objFso, myFile1,myFile2, myFile3, myFile4
myFile1=”C:Documents and Settings1 Desktopabcd.txt”
myFile2=”C:Documents and Settings1 Desktopabcd.doc”
myFile3=”C:Documents and Settings1 Desktopabcd.xls”
myFile4=”C:Documents and Settings1 Desktopabcd.pdf”
Set objFso=CreateObject(“scripting.FileSystemObject”)
If Not objFso.FileExists(myFile1) Then
objFso.CreateTextFile (myFile1)
End If
If Not objFso.FileExists(myFile2) Then
objFso.CreateTextFile (myFile2)
End If
If Not objFso.FileExists(myFile3) Then
objFso.CreateTextFile (myFile3)
End If
If Not objFso.FileExists(myFile4) Then
objFso.CreateTextFile (myFile4)
End If
ix) Read Data Character by Character from a text file
Dim objFso, myFile, myChar
Set objFso=CreateObject(“scripting.FileSystemObject”)
Set myFile=objFso.OpenTextFile(“C:Documents and Settings1 Desktopabcd.txt”,1) ’1 for Read, 2 for Write and 8 for Append
Do Until myFile.AtEndOfStream=True
myChar=myFile.Read(1)
Msgbox myChar
Loop
myFile.Close
Set objFso=Nothing
x)Read Line by Line from a Text File
Dim objFso, myFile, myChar
Set objFso=CreateObject(“scripting.FileSystemObject”)
Set myFile=objFso.OpenTextFile(“C:Documents and Settings1 Desktopabcd.txt”,1) ’1 for Read, 2 for Write and 8 for Append
Do Until myFile.AtEndOfStream=True
myChar=myFile.ReadLine
Msgbox myChar
Loop
myFile.Close
Set objFso=Nothing
xi) Data Driven Testing by fetching Test data directly from a Text file.
'''save the file with read and write protected'''''pwd1 is for read protected pwd2 is for write protected
Set xl=CreateObject("Excel.Application")Set wb=xl.Workbooks.Addxl.DisplayAlerts=False Set ws=wb.Worksheets("sheet1")ws.cells(1,1)=100ws.cells(1,2)=200wb.Saveas "e:\data2.xls",,"pwd1","pwd2"wb.CloseSet xl=nothing
'''Script to open excel file ,which is read and write protected write data'''''pwd1 is for read protected pwd2 is for write protected
Set xl=CreateObject("Excel.Application")Set wb=xl.Workbooks.Open("e:\data2.xls",0,False,5,"pwd1","pwd2")xl.DisplayAlerts=False Set ws=wb.Worksheets("sheet1")ws.cells(1,2)="hello"ws.cells(2,2)="new data"wb.Savewb.CloseSet xl=nothing ''Script to get the list of links in Google and do spell check===============================================================dim dset mw=CreateObject("Word.Application")set d=Description.Created("micclass").value="Link"set a=Browser("Google").page("Google").childobjects(d)for i=0 to a.count-1 mw.WordBasic.filenew s=a(i).getROProperty("innertext")mw.WordBasic.insert s if mw.ActiveDocument.Spellingerrors.count>0 then Reporter.ReportEvent 1,"Spelling","spelling error :"&s end ifmw.ActiveDocument.Close(False)next
mw.quitset mw=nothing=========================================''''Script to check ON the checkboxes in yahoo mail inbox=========================================Dim dSet d=Description.Created("micclass").value="WebCheckBox"Set c=Browser("Inbox (17) - Yahoo! Mail").Page("Inbox (17) - Yahoo! Mail").ChildObjects(d)For i=1 to 10c(i).set "ON"Next========================================'''script to select a mail having subject 'hi' or 'HI'========================================n=Browser("yahoo").Page("yahoo").WebTable("Inbox").RowCountFor i=2 to ns=Browser("yahoo").Page("yahoo").WebTable("Inbox").GetCellData(i,7)If lcase(trim(s))="hi" ThenBrowser("yahoo").Page("yahoo").WebCheckBox("index:="&i-1).set "ON"End IfNext========================================'''''Function to send a mail========================================Function SendMail(SendTo, Subject, Body, Attachment) Set otl=CreateObject("Outlook.Application")Set m=otl.CreateItem(0) m.to=SendTo m.Subject=Subject m.Body=Body If (Attachment <> "") Then Mail.Attachments.Add(Attachment)End Ifm.Send otl.Quit Set m = Nothing Set otl = NothingEnd FunctionCall SendMail("[email protected]","hi","This is test mail for testing","")'''''''''''''''create a new text file=====================================================Dim fs,fSet fs=CreateObject("Scripting.FileSystemObject")Set f=fs.CreateTextFile("e:\file1.txt")
f.WriteLine "hello"f.WriteLine "this is sample data"f.CloseSet fs=nothing====================================================='''''''''''''''read data from a text file=====================================================Dim fs,fSet fs=CreateObject("Scripting.FileSystemObject")Set f=fs.OpenTextFile("e:\file1.txt",1)While f.AtEndOfLine<>Truemsgbox f.ReadLineWendf.CloseSet fs=nothing=====================================================''''''''''create a new excel file and write data=====================================================Dim xl,wb,wsSet xl=CreateObject("Excel.Application")Set wb=xl.Workbooks.AddSet ws=wb.Worksheets("sheet1")ws.cells(1,1)=10ws.cells(2,1)=20ws.cells(3,1)=50wb.SaveAs "e:\file1.xls"wb.CloseSet xl=nothing====================================================='''''''open existing file and write data in second column in Sheet1=====================================================Dim xl,wb,wsSet xl=CreateObject("Excel.Application")Set wb=xl.Workbooks.Open("e:\file1.xls")Set ws=wb.Worksheets("sheet1")ws.cells(1,2)="Testing"ws.cells(2,2)="hyd"ws.cells(3,2)="ap"wb.Savewb.CloseSet xl=nothing====================================================='''''''''''read data from excel from rows and columns=====================================================Dim xl,wb,wsSet xl=CreateObject("Excel.Application")
Set wb=xl.Workbooks.Open("e:\file1.xls")Set ws=wb.Worksheets("sheet1")r=ws.usedrange.rows.countc=ws.usedrange.columns.countFor i=1 to r v="" For j=1 to c v=v&" "& ws.cells(i,j) Next print v print "-----------------------"Nextwb.CloseSet xl=nothing======================================================''''''''''''''''get the bgcolor in a cell in excel======================================================Dim xl,wb,wsSet xl=CreateObject("Excel.Application")Set wb=xl.Workbooks.Open("e:\file3.xls")Set ws=wb.Worksheets("sheet1")r=ws.usedrange.rows.countc=ws.usedrange.columns.countFor i=1 to rFor j=1 to c x=ws.cells(i,j).interior.colorindexmsgbox xNextNextwb.CloseSet xl=nothing=======================================================''''''''''''''''''''''create word and write data=======================================================dim mwset mw=CreateObject("Word.Application")mw.Documents.Addmw.selection.typetext "hello"mw.ActiveDocument.SaveAs "e:\file1.doc"mw.quitset mw=nothing=======================================================''''''''''script will display all the doc files in all the drives in the system========================================================Dim mwSet mw=CreateObject("Word.Application")
Set fs=createobject("Scripting.FileSystemObject")Set d=fs.Drivesmw.FileSearch.FileName="*.doc"For each dr in dmsgbox drmw.FileSearch.LookIn=drmw.FileSearch.SearchSubFolders=Truemw.FileSearch.Execute For each i in mw.FileSearch.FoundFiles print i Set f=fs.GetFile(i)print f.Name&" "&f.Size&" "&f.DateCreated print "-------------------------------------------------------------------"NextNextmw.Quit=========================================================='''''''''Open Internet Explorer and navigate to yahoomail==========================================================Dim ieSet ie=CreateObject("InternetExplorer.Application")ie.Visible=Trueie.Navigate "www.yahoomail.com"x=Browser("CreationTime:=0").GetROProperty("title")msgbox x==========================================================''''''Create word, Create table and write all the services names==========================================================Set mw = CreateObject("Word.Application")mw.Visible = TrueSet dc = mw.Documents.Add()Set objRange = dc.Range()dc.Tables.AddobjRange,1,3Set objTable = dc.Tables(1)x=1strComputer = "."Set wms=GetObject("winmgmts:\\" & strComputer & "\root\cimv2")Set colItems = wms.ExecQuery("Select * from Win32_Service")For Each s in colItemsIf x > 1 Then objTable.Rows.Add() End If objTable.Cell(x, 1).Range.Font.Bold = True objTable.Cell(x, 1).Range.Text = s.NameobjTable.Cell(x, 2).Range.text = s.DisplayName
objTable.Cell(x, 3).Range.text = s.Statex = x + 1Next
*******************************************************************************************'How do we validate links in web page and how to display linknames with status in excel
On error resume next 'To handle run time error''SystemUtil.CloseProcessByName "iexplore.exe" Systemutil.Run "iexplore","https://login.sample.com/amserver/UI/Login"Browser( "sample").Page("sample ").WebEdit("html id:=IDToken1").Set "56793287"Browser("sample ").Page("sample ").WebEdit("html id:=IDToken2").Set "ranao2009"Browser("sample ").Page("sample ").WebButton("html id:=signIn").Clickwait(10)Browser("sample ").Dialog("text:=Security Information").Winbutton("text:=&Yes").clickIf Browser("sample ").Page("sample ").Image("close_text_button").Exist(10) ThenBrowser("sample ").Page("sample ").Image("close_text_button").ClickEnd ifSet objLinkDesc=Description.CreateobjLinkDesc("micclass").value="Link"'To count no of links in a web pageSet objLinks = Browser("sample ").Page("sample ").ChildObjects(objLinkDesc)'Set objLinks=Browser("sample ").page("sample ").childobjects(objLinkDesc)msgbox objLinks.countFor i=1 to objLinks.count-187 step 1 strTargetUrl=Browser("title:=.*").Page("title:=.*").Link("text:=.*","index:="&i).GetRoProperty("url") print(strTargetUrl)' Reporter.ReportEvent micDone,""&strTargetUrl,""' objLinks(i).click Browser("title:=.*").Page("title:=.*").Link("text:=.*","index:="&i).click Browser("title:=.*").Sync strActualUrl=Browser("title:=.*").GetROProperty("url") Print(strActualUrl) 'Verify Link is navigating into the correct page If instr(strTargetUrl,strActualUrl)>0 Then Reporter.ReportEvent micPass,"Navigate to correct page","The Actual URL is"& vbcrlf &strActualUrl& vbcrlf &"The Target URL is"& vbcrlf &strTargetUrl 'Report result to the QTP test log Else Reporter.ReportEvent micFail,"Navigate to wrong page","The Actual URL is"& vbcrlf &strActualUrl& vbcrlf &"The Target URL is"& vbcrlf &strTargetUrl 'Report result to the QTP test log
End If Browser("title:=.*").Page("title:=.*").Link("name:=SampleHomeLogo").click Browser("title:=.*").SyncNext