Properties of the Folder Object

The Folder object provides access to all of the properties of a specified folder. The following lines of code create a reference to the Folder object:

Set fs = CreateObject("Scripting.FileSystemObject") Set objFolder = fs.GetFolder("C:\My Documents")

Table 8-5 gives examples of VBA procedures that access the Folder object. Table 8-5: Selected properties of the Folder object

Property Description

Attributes Folder attributes

DateCreated Folder creation date

Drive Name of the folder

Files Collection of files in the folder

Sub CountFilesInFolder()

Dim fs, strFolder, objFolder, colFiles

IsRootFolder Name

ParentFolder

Path

Size strFolder = InputBox("Enter the folder name:") If Not IsFolderEmpty(strFolder) Then

Set fs = CreateObject("Scripting.FileSystemObject") Set objFolder = fs.GetFolder(strFolder) Set colFiles = objFolder.Files

MsgBox "The number of files in the folder " & _ strFolder & " = " & colFiles.Count

End If

End Sub

The above procedure calls the IsFolderEmpty function, which is discussed with the Size property in this table.

Returns True if the folder is the root folder

The name of the folder

The parent folder of the specified folder

The full path to the folder

Folder size in bytes

Function IsFolderEmpty(myFolder) Dim fs, objFolder

Set fs = CreateObject("Scripting.FileSystemObject") Set objFolder = fs.GetFolder(myFolder) IsFolderEmpty = (objFolder.Size = 0) End Function

SubFolders Collection of subfolders in the folder Type Folder type (e.g., File folder or Recycle Bin)

0 0

Post a comment