Previous Page
Next Page

Binding to Folders

To gain information about the properties or attributes of a folder, you must first bind to the folder. Because the file system object represents folders as Component Object Model (COM) objects, you must create a reference to them prior to connecting to themthat is, you must bind to them. You already know that to create or delete a folder, you have to create an instance of FileSystemObject. After you do that, you use the GetFolder method to connect to the folder.

Just the Steps

To bind to a folder

1.
Implement the FileSystemObject by using CreateObject.

2.
Specify the path to the folder.

3.
Use Set keyword to assign the path to a variable.


In the following script, you implement FileSystemObject by using CreateObject. Next, you use the GetFolder method to bind to the folder called fso found in the C drive.

BindFolder.vbs

Set objFSO = CreateObject("Scripting.filesystemobject")
Set objFolder = objFSO.getfolder("c:\fso")
WScript.Echo("folder is bound")

Does the Folder Exist?

Binding to a folder in and of itself is rather boring, but what if the folder does not exist? If you try to bind to a folder that does not exist, the script generates an error message, and your script might fail. The "path not found" error can be prevented from occurring by using the FolderExists method. In the CreateBasicFolder_checkFirst.vbs script, you check for the existence of a folder prior to creating a new one.

By incorporating the FolderExists method into the CreateBasicFolder vbs script to create new folders, you gain the ability to delete the existing folder prior to creating a new one. One situation in which this ability would be useful would be when creating a folder for logging onto a workstation. If a previous logging folder were found, that folder could be deleted to make room for a new folder. If you don't want to delete the folder, if that folder exists, you simply omit the DeleteFolder command from the script and modify the message displayed to the user. In other situations, the mere presence of a folder is all you need. If you create a folder called RasErrors, when a user fails to make a remote connnection to the network, then the presence of this folder could indicate that the user had a problem connecting remotely.

CreateBasicFolder_checkFirst.vbs

Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists ("C:\fso1") Then
  WScript.Echo("folder exists and will be deleted")
  objFSO.DeleteFolder ("C:\fso1")
  WScript.Echo("clean folder created")
  Set objFolder = objFSO.CreateFolder("C:\fso1")
Else
  WScript.Echo("folder does not exist and will be created")
  Set objFolder = objFSO.CreateFolder("C:\fso1")
End if


Previous Page
Next Page