Verifying That a Folder Exists

Microsoft® Windows® 2000 Scripting Guide

Most folder operations, including copying, moving, and deleting, require the specified folder to exist before the operation can be carried out; after all, a script cannot copy, move, or delete a folder that does not exist. If the script attempts to bind to a nonexistent folder, the script will fail with a "Path not found" error.

To avoid this problem, you can use the FolderExists method to verify that a folder exists before attempting to bind to it. FolderExists takes a single parameter (the path name to the folder) and returns a Boolean value: True if the folder exists, False if the folder does not.

For example, in the script in Listing 4.6, the FolderExists method is used to verify the existence of the folder C:\FSO. If FolderExists equals True, the script uses the GetFolder method to bind to the folder. If FolderExists is False, the script echoes the message "Folder does not exist."

Listing 4.6 Verifying the Existence of a Folder

  
1
2
3
4
5
6
7
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists("C:\FSO") Then
 Set objFolder = objFSO.GetFolder("C:\FSO")
 Wscript.Echo "Folder binding complete."
Else
 Wscript.Echo "Folder does not exist?"
End If