Monday, June 8, 2020

VBA FSO GetSpecialFolder function

FileSystemObject has many important methods. GetSpecialFolder is one of them.

GetSpecialFolder method is used to deal with three kinds of Windows special folders. They are as follows:
  1. Windows folders
  2. System folders
  3. Temporary folders
Windows folders are the folders which contain files installed during by the Windows operating system when a program is installed.
The System folder contains libraries, fonts, and device drivers.
The Temp folder is used to store temporary files of a program. Its path is found in the TMP environment variable.

Constant
Value
Description
WindowsFolder
0
The Windows folder contains files installed by the Windows operating system.
SystemFolder
1
The System folder contains libraries, fonts, and device drivers.
TemporaryFolder
2
The Temp folder is used to store temporary files. Its path is found in the TMP environment variable.

Example 1

Sub GetWindowsSubFolders()
    Dim fso As Object ''New Scripting.FileSystemObject
    Dim specialf As Object ''Folder
    Dim fd As Object ''Folder

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set specialf = fso.GetSpecialFolder(0)

    For Each fd In specialf.SubFolders
        Debug.Print fd.Path
    Next

    Set specialf = Nothing
    Set fso = Nothing

End Sub

Example 2

Sub GetSystemSubFolders()
    Dim fso As Object ''New Scripting.FileSystemObject
    Dim specialf As Object ''Folder
    Dim fd As Object ''Folder

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set specialf = fso.GetSpecialFolder(1)

    For Each fd In specialf.SubFolders
        Debug.Print fd.Path
    Next

    Set specialf = Nothing
    Set fso = Nothing

End Sub

No comments:

Post a Comment

Hot Topics