Binding to a Folder

Microsoft® Windows® 2000 Scripting Guide

In the Windows Shell, folders are Component Object Model (COM) objects. This means that, before you can access the properties of an individual folder, you must create an object reference to that folder, a process commonly referred to as binding. You can bind to a folder by creating an instance of the FileSystemObject and then using the GetFolder method to connect to the folder.

When using the GetFolder method, you must:

  • Specify the path name to the folder. The path can be referenced by either a local path or a UNC path (for example, \\accounting\receivables). However, you cannot use wildcards within the path name. In addition, you cannot create a single object reference that binds to multiple folders at the same time. A code statement similar to the following results in a compilation error:

    objFSO.GetFolder("C:\FSO", "C:\Scripts")
    

    If you need to work with multiple folders, you either need to use WMI (which can return a collection of folders) or create a separate object reference for each folder.

  • Use the Set keyword when assigning the path to a variable. The Set keyword is required because it indicates that the variable in question is an object reference.

For example, the script in Listing 4.5 binds to the folder C:\FSO.

Listing 4.5 Binding to a Folder

  
1
2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\FSO")

Although wildcard characters are not allowed, you can use the dot (.) to bind to the current folder, dot-dot (..) to bind to the parent folder of the current folder, and the backslash (\) to bind to the root folder. For example, the following code statement binds to the current folder:

Set objFolder = objFSO.GetFolder(".")