Microsoft Office 2000/Visual Basic Programmer's Guide   

Returning Files from the File System

Once you've created a new instance of the FileSystemObject, you can use it to work with drives, folders, and files in the file system.

The following procedure returns the files in a particular folder into a Dictionary object. The GetFiles procedure takes three arguments: the path to the directory, a Dictionary object, and an optional Boolean argument that specifies whether the procedure should be called recursively. It returns a Boolean value indicating whether the procedure was successful.

The procedure first uses the GetFolder method to return a reference to a Folder object. It then loops through the Files collection of that folder and adds the path and file name for each file to the Dictionary object. If the blnRecursive argument is set to True, the GetFiles procedure is called recursively to return the files in each subfolder.

Function GetFiles(strPath As String, _
                dctDict As Dictionary, _
                Optional blnRecursive As Boolean) As Boolean
              
   ' This procedure returns all the files in a directory into
   ' a Dictionary object. If called recursively, it also returns
   ' all files in subfolders.
   
   Dim fsoSysObj      As FileSystemObject
   Dim fdrFolder      As Folder
   Dim fdrSubFolder   As Folder
   Dim filFile        As File
   
   ' Return new FileSystemObject.
   Set fsoSysObj = New FileSystemObject
   
   On Error Resume Next
   ' Get folder.
   Set fdrFolder = fsoSysObj.GetFolder(strPath)
   If Err <> 0 Then
      ' Incorrect path.
      GetFiles = False
      GoTo GetFiles_End
   End If
   On Error GoTo 0
   
   ' Loop through Files collection, adding to dictionary.
   For Each filFile In fdrFolder.Files
      dctDict.Add filFile.Path, filFile.Path
   Next filFile

   ' If Recursive flag is true, call recursively.
   If blnRecursive Then
      For Each fdrSubFolder In fdrFolder.SubFolders
         GetFiles fdrSubFolder.Path, dctDict, True
      Next fdrSubFolder
   End If

   ' Return True if no error occurred.
   GetFiles = True
   
GetFiles_End:
   Exit Function
End Function

You can use the following procedure to test the GetFiles procedure. This procedure creates a new Dictionary object and passes it to the GetFiles procedure.

Sub TestGetFiles()
   ' Call to test GetFiles function.

   Dim dctDict As Dictionary
   Dim varItem As Variant
   
   ' Create new dictionary.
   Set dctDict = New Dictionary
   ' Call recursively, return files into Dictionary object.
   If GetFiles(GetTempDir, dctDict, True) Then
      ' Print items in dictionary.
      For Each varItem In dctDict
         Debug.Print varItem
      Next
   End If
End Sub

Both of these procedures are available in the modFiles module in VBA.mdb in the ODETools\V9\Samples\OPG\Samples\CH07 subfolder on the Office 2000 Developer CD-ROM.

You can also use the Office FileSearch object, discussed in Chapter 6, "Working with Shared Office Components," to find a file or group of files. The FileSearch object has certain advantages in that you can search subfolders, search for a particular file type, or search the contents of a file by simply setting a few properties.

On the other hand, the Microsoft Scripting Runtime object library allows you to work with individual files or folders as objects that have their own methods and properties. For example, the ChangeFileAttributes procedure in the following section, "Setting File Attributes," alters file attributes by setting the Attributes property for each File object in the Files collection of a particular Folder object.