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

PDF Automation in QTP

                                                                            The most challenging issue with PDFs is that it could be of any kind, not just a tabular data; it could have plain text, images or even forms to fill up. So this makes a tester’s life a bit difficult, never mind, we will definitely find an easy of do it… Although there are already some better approaches we have to deal with PDF documents but I found many of us are facing so many difficulties using this. There are lots of queries coming at QTP forums asking for an easy way of doing it with PDFs. keeping those in my mind I started c...

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 :  ...

Download Test Resource From QC Using QTP

'########################################################################### '* Function Name: QCGetResource '* Designer: Jay '* Date 09-May-2012 '* This script will Download QC Test Resource to a local dir '########################################################################### Function QCGetResource(resourceName,saveTo)     Set qcConn = QCUtil.QCConnection     Set oResource = qcConn.QCResourceFactory     Set oFilter = oResource.Filter     oFilter.Filter("RSC_FILE_NAME") = resourceName     Set oResourceList = oFilter.NewList     If oResourceList.Count = 1 Then         Set oFile = oResourceList.Item(1)         oFile.FileName = resourceName         oFile.DownloadResource saveTo, True     End If         Set qcConn = Nothing     Set oResource = Nothi...

compare Two Text files using Vb Script

Public Function CompareFiles (FilePath1, FilePath2) Dim FS, File1, File2 Set FS = CreateObject(“Scripting.FileSystemObject”) If FS.GetFile(FilePath1).Size <> FS.GetFile(FilePath2).Size Then CompareFiles = True Exit Function End If Set File1 = FS.GetFile(FilePath1).OpenAsTextStream(1, 0) Set File2 = FS.GetFile(FilePath2).OpenAsTextStream(1, 0) CompareFiles = False Do While File1.AtEndOfStream = False Str1 = File1.Read(1000) Str2 = File2.Read(1000) CompareFiles = StrComp(Str1, Str2, 0) If CompareFiles <> 0 Then CompareFiles = True Exit Do End If Loop File1.Close() File2.Close() End Function Return value: The function returns 0 or False if the two files are identical, otherwise True. Example: File1 = “C:\countries\apple1.jpg” File2 = “C:\countries\apple3.jpg” If CompareFiles(File1, File2) = False Then MsgBox “Files are identical.” Else MsgBox “Files are different.” End If    Source: Mercury Forum’s KB articles

CreateImageFromClipBoard using QTP

'-------------------------------------------------------------------------' Method : CreateImageFromClipBoard' Author : Jai Purpose : It gets the clipboard image and convert as a image file.' Parameters: FileName - String, contains the BMP file name' iIndex - Integer, contains the Worksheet index' Returns : String. The replaced file name it gives.' Caller : - Nil' Calls : - Nil' ------------------------------------------------------------------------- Sub CreateImageFromClipBoard(sFileName) Dim wshShell,ShellReturnCode, sCmdExec Set WshShell = WScript.CreateObject("WScript.Shell") sCmdExec = "D:\autostuff\i_view32.exe /silent /clippaste /convert="& sFileName ShellReturnCode = WshShell.Run(sCmdExec, 1, True) End Sub