Skip to main content

FileSystemObject..

Managing Files and Folders
As you have already seen this afternoon the FileSystemObject is actually fairly easy to work with. All that you have to do is establish an instance of it and you can start using its properties and methods. In the sections that follow you’ll see examples of how to use this object to create, copy, move, and delete files and folders.
Copying Files
Using the FileSystemObject object’s CopyFile() method you can copy one or more files. For example, you might want to copy all the files in the folder on your computer to a network drive at the end of each day. I’ll show you how to work with network drives this evening. For now let’s just focus on how the CopyFile() method works.The first step in copying a file is to set up an instance of the FileSystemObject. Then you can execute its CopyFile() method as shown here.

Set fsoObject = CreateObject("Scripting.FileSystemObject")

fsoObject.CopyFile("d:\ displayText.txt", "d:\myDocs\ displayText.txt")

In this example, a file named myFile.txt is copied from the root folder on the C: drive to a folder named myDocs located on the computer’s D: drive. You can modify this example to copy more than one file using wildcard characters as shown in the next example. Here all files in the root directly with a filename of myFile are copied to the destination folder regardless of their file extension.

Set fsoObject = CreateObject("Scripting.FileSystemObject")

fsoObject.CopyFile("d:\ displayText.*", "d:\Docs")

Moving Files

Moving files is similar to copying them except that instead of leaving the original file in place and placing a duplicate copy in the destination location, the original file is moved leaving only one copy of the file. You can move one or more files using the FileSystemObject object’s MoveFile() method.For example, you can move all files with a .txt file extension found in the root of the C: drive to a myDocs folder on the D: drive using the following VBScript statements.

Set fsoObject = CreateObject("Scripting.FileSystemObject")

fsoObject.MoveFile("c:\*.txt", "c:\Docs")

Deleting Files
You can use the FileSystemObject object’s DeleteFile() method to delete one or more files. For example, you might want to write a script that cleans out a folder at the end of each day or that deletes files after reading and processing them. You can delete one or more files as demonstrated here.

Set fsoObject = CreateObject("Scripting.FileSystemObject")

fsoObject.DeleteFile("d:\*.txt")

Here all files that have a .txt file extension located in the root of the C: drive will be deleted.
Creating a Folder
Working with folders is similar to working with files. You can use the FolderExists() method of the FileSystemObject to determine if a folder exists. If the folder does not exist, you can create it using the FileSystemObject object’s CreateFolder() method as demonstrated here.

Set fsoObject = CreateObject("Scripting.FileSystemObject")

If (fsoObject.FolderExists("d:\Docs") = false) Then

Set myFolder = fsoObject.CreateFolder("d:\Docs")

End If

The first thing that this example does is check to see if a folder named myDocs already exists on the computer’s D: drive. If it does not exist, then the folder is created. Otherwise nothing happens.
Copying Folders
Copying a folder is pretty much the same as copying a file. The folder and all its contents are copied to a new location leaving the original copy still in place. You can copy folders using the FileSystemObject object’s CopyFolder() method.Take a look at the following example. It copies a folder named myDocs located on the computer’s D: drive to its C: drive.

Set fsoObject = CreateObject("Scripting.FileSystemObject")

fsoObject.CopyFolder("c:\Docs", "d:\Docs")

As you can see the CopyFolder() methods requires two arguments, the source and destination folder names, including their complete paths. By changing the name assigned to the destination folder, you can rename the folder as part of the copy operation as demonstrated here.

Set fsoObject = CreateObject("Scripting.FileSystemObject")

fsoObject.CopyFolder("c:\Docs", "d:\NewDocs")

If the destination folder already exists, then the contents of the source folder are copied into it alongside its current contents. The CopyFolder() method supports an additional third parameter that allows you to tell it what to do if the destination folder contains files with duplicate filenames of those found in the source folder. This parameter is set to either a value of true or false. Setting it to true causes any matching files to be overridden. Setting it to false prevents this from happening. Let’s look at a couple examples. The first example prevents files with duplicate filenames from being overridden.

Set fsoObject = CreateObject("Scripting.FileSystemObject")

fsoObject.CopyFolder("c:\Docs", "d:\Docs", "False")

The second example allows files with duplicate names to be overridden in the destination folder.

Set fsoObject = CreateObject("Scripting.FileSystemObject")

fsoObject.CopyFolder("c:\Docs", "d:\Docs", "True")

Moving Folders
Moving a folder works pretty much the same as copying one except that moving the folder leaves you with only the one copy. You can move folders using the FileSystemObject object’s MoveFolder() method. This method moves a folder and all its contents, including subfolders, to a new destination.For example, the following VBScript statements move a folder called myDocs from the computer’s D: drive to its C: drive.

Set fsoObject = CreateObject("Scripting.FileSystemObject")

fsoObject.MoveFolder("c:\Docs", "d:\Docs")

If a folder with the same name already exists at the root of the computer’s D: drive, then the contents of the source folder will be copied into the existing destination along side its current contents.

Deleting Folders

You can delete one or more folders using the FileSystemObject object’s DeleteFolder() method. This method deletes a folder and all its contents, including subfolders. For example, the following VBScript statements can be used to delete a folder named myDocs located on a computer’s D: drive.

Set fsoObject = CreateObject("Scripting.FileSystemObject")

fsoObject.DeleteFolder("d:\Docs")

Managing Files with the File Object

The File Object allows you to work with one file at a time as opposed to the FileSystemObject, which lets you manage multiple files. Using the File object instead of the FileSystemObject requires a little more work. You still have to create an instance of the FileSystemObject to interact with the file system. You also need to use the FileSystemObject object’s GetFile() method to retrieve a File object that represents the file that your script will be managing. Once these two things are set up you can execute any of the File object’s methods.

Copying a File

Using the File object’s Copy() method you can copy a file from one location to another. This method does not support the use of wildcard characters and cannot therefore be used to copy multiple files.The following example demonstrates how to use the File object’s Copy() method to copy a file named myFile.txt from the myDocs folder on the computer’s D: drive to the root of the C: drive.

Set fsoObject = CreateObject("Scripting.FileSystemObject")

Set source_file = fsoObject.GetFile("c:\Docs\ displayText.txt")

source_file.Copy("d:\")

In this example, the FileSystemObject object’s GetFile() method is used to establish a reference to the file that is to be copied. Then the Copy() method is used to set the destination where the file is to be copied.

Moving a File

As you probably expect by now, moving a file using the File object’s Move() method works almost exactly like copying it using the Copy() method. For example, the following VBScript statements demonstrate how to move a file from one location to another.

Set fsoObject = CreateObject("Scripting.FileSystemObject")

Set source_file = fsoObject.GetFile("c:\Docs\ displayText.txt")

source_file.Move("d:\")

Deleting a File

You can delete an individual file using the File Object’s Delete() method. Like the File object’s other methods, the Delete() method does not support wildcard characters limiting it to being able to delete just one file at a time.Take a look at the following example. Here a file named myFile.txt is deleted from a folder named myDocs located on the computer’s D: drive.

Set fsoObject = CreateObject("Scripting.FileSystemObject")

Set source_file = fsoObject.GetFile("c:\Docs\ displayText.txt ")

source_file.Delete()

Managing Folders with the Folder Object
The Folder object is similar to the File object only it works with one folder at a time instead of one file at a time. Like the File object, it requires a little more work to set up than does simply using the FileSystemObject. First, you’ll need to instantiate the FileSystemObject. Then you must use the FileSystemObject object’s GetFolder() method to retrieve a Folder object that represents the folder that your script will be working with. Once these two things are set up you can execute any of the Folder object’s methods.

Copying a Folder

Like the File object, the Folder object supports the Copy() method. Because it doesn’t support wildcard characters it is useful only when you want to work with a single folder at a time. The Copy() method copies the folder and all its contents, including any subfolders, to a new location.For example, the following VBScript statements demonstrate how to use the Copy() method. In this case a folder named myDocs is copied from the root of the computer’s D: drive to the root of the computer’s C: drive.

Set fsoObject = CreateObject("Scripting.FileSystemObject")

Set source_folder = fsoObject.GetFolder("d:\Docs")

source_folder.Copy("c:\")

Moving a Folder
The Folder object’s Move() method lets you move a folder from one location to another. This method recursively copies a folder and all its contents. Because it does not support wildcard characters, this method can only move one folder at a time, as opposed to the FileSystemObject object’s MoveFolder() method, which can move any number of folders in a single operation.The following example demonstrates how to use this method to move a folder. In this case the folder is named myDocs and it is moved from the computer’s D: drive to its C: drive.

Set fsoObject = CreateObject("Scripting.FileSystemObject")

Set source_folder = fsoObject.GetFolder("d:\Docs")

source_folder.Move("c:\")

Deleting a Folder

You can delete a folder using the Folder object’s Delete() method. Wildcard character matching is not supported so it only works with one folder at a time. If you need to delete more than one folder you can use the FileSystemObject object’s DeleteFolder() method, discussed earlier in this chapter.This method deletes the specified folder and all its contents, including subfolders. Its use is demonstrated here.

Set fsoObject = CreateObject("Scripting.FileSystemObject")

Set source_folder = fsoObject.GetFolder("d:\Docs")

source_folder.Delete()

Comments

Popular posts from this blog

Convert JSON to XML using QTP/UFT/VBScript

Sample Code : Dim strPage,strJSON,objIE strPage = "C:\Jay\JLoader.html" Set objIE = CreateObject("InternetExplorer.Application") objIE.Visible = True objIE.Navigate2 strPage While objIE.Busy : Wend strJSON = "{""FirstName"":""Jay"", ""LastName"":""Krishna""}" Set objWin = objIE.document.parentWindow objWin.execScript "var jsonStr2XML = function(strJSON) { return json2xml(JSON.parse(strJSON));};" Msgbox  oWin.jsonStr2XML(strJSON) objIE.Quit In Detail: Converting The most popular data interchange format JSON(JavaScript Object Notation) to XML using QTP/UFT. Parsing JSON in UFT could be a challenge so we will use JavaScript in UFT to make it perfect. SO We need :              Java Script API  - To Convert JSON to XML                         JavaScript Files :                         http://goessner.net/download/prj/jsonxml/j

Read Outlook mail attachment and Body using Vb Script or QTP

Set olApp = CreateObject("Outlook.Application") Set olns = olApp.GetNameSpace("MAPI") Set ObjFolder = olns.GetDefaultFolder(6) j = 0 For each item1 in ObjFolder.Items        iattachCnt = item1.Attachments.Count     Print "Attachments Count: " & iattachCnt     For i = 1 to iattachCnt         Print "FileName :    " & item1.Attachments(i).FileName         Print "Display Name:   " & item1.Attachments(i).DisplayName         Print "Size: " & item1.Attachments(i).Size     Next     Print " Body : " & item1.body     Print "--------------------------------------Mail Num - " & j & " -----------------------------------------------"     j = j+1    Next

Excel Sorting By Rows and Columns

Excel Sorting By Row: Const xlAscending = 1 Const xlNo = 2 Const xlSortRows = 2 Set objExcel = CreateObject(“Excel.Application”) objExcel.Visible = True Set objWorkbook = objExcel.Workbooks.Open(“C:\Jay\Docs1.xls”) Set objWorksheet = objWorkbook.Worksheets(1) objWorksheet.Cells(1,1).activate Set objRange = objExcel.ActiveCell.EntireRow objRange.Sort objRange, xlAscending, , , , , , xlNo, , , xlSortRows set objExcel=nothing Excel Sorting By Column : Const xlAscending = 1′represents the sorting type 1 for Ascending 2 for Desc Const xlYes = 1 Set objExcel = CreateObject(“Excel.Application”)’Create the excel object objExcel.Visible = True’Make excel visible Set objWorkbook = _ objExcel.Workbooks.Open(“C:\Jay\Docs1.xls”)’Open the document Set objWorksheet = objWorkbook.Worksheets(1)’select the sheet based on the index .. 1,2 ,3 … Set objRange = objWorksheet.UsedRange’which select the range of the cells has some data other than blank Set objRange2 = objExcel.Range

How to Read or Select Context Menu or Right Click Menu using QTP.

Select The Item in Right Click Menu or Context Menu: Window("sampleWindow").WinMenu("MenuObjType:=1).Select"File;New" Here MenuObjtype can be 1 r 2 r 3 .......n Check wether the Item is Exist or Not: If Window("sampleWindow").WinMenu("MenuObjType:=1).GetItemProperty("1","Exist") Then   Msgbox"Exist" Else  Msgbox"Does Not Exist" End If                                         Or If Window("sampleWindow").WinMenu("MenuObjType:=1).GetItemProperty("File","Exist") Then   Msgbox"Exist" Else  Msgbox"Does Not Exist" End If Get the Items in Context Menu: For i = 1 to 10 Print  Window("sampleWindow").WinMenu("MenuObjType:=" & i).GetItemProperty("1","Label") Then Next

How to Download a file using VbScript

Following is the code to download a file using Vbscript, without using QTP This code uses the HTMLDom and URLDownloadToFile method from urlmon API. Since VBScript does support calling Native API methods directly, here I am using  Excel macro to declare a function for the urlmon API and running the macro by Excel API from VBscript Step1: Create a new excel and open the visual basic editor, Insert Module and paste the following code the Module, save the excel file Private Declare Function URLDownloadToFile Lib “urlmon” Alias _                                            “URLDownloadToFileA” ( _                                            ByVal pCaller As Long, ByVal szURL As String, _                                            ByVal szFileName As String, _                                            ByVal dwReserved As Long, _                                            ByVal lpfnCB As Long) As Long Sub FileSave(strUrl, Des)     r = URLDownloadToFile(0, strUrl, Des, 0, a)