Using the File System Object (FSO) for Retrieving Folder Attributes

Here’s a simple function to get the attributes of a folder, such as:

  • Read-Only
  • Hidden
  • System
  • Volume
  • Archive
  • Compressed
  • and more…

 

'---------------------------------------------------------------------------------------
' Procedure : FSO_GetoFolderAttributes
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : List the attributes of the specified folder
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
'             (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Late Binding  -> None required
'             Early Binding -> Microsoft Scripting Runtime
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sFolderPath   : Path of the folder you wish to check the attributes of
' sDelimiter    : Delimiter to be used to separate the attributes, default to a comma
'
' Usage:
' ~~~~~~
' ? FSO_GetoFolderAttributes("C:\MSOCache")
'   Returns -> ReadOnly, Hidden, Directory
'
' ? FSO_GetoFolderAttributes("C:\Windows")
'   Returns -> Directory
'
' ? FSO_GetoFolderAttributes("C:\System Volume Information")
'   Returns -> Hidden, System, Directory
'
' Revision History:
' Rev       Date(yyyy-mm-dd)        Description
' **************************************************************************************
' 1         2009-07-23
' 2         2025-01-30              Updated header to new format
'                                   Renamed function to follow conversion
'                                   Added new constants
'                                   Added Early/Late binding CCD
'---------------------------------------------------------------------------------------
Function FSO_GetoFolderAttributes(ByVal sFolderPath As String, _
                                  Optional sDelimiter As String = ", ") As String
    #Const FSO_EarlyBind = False    'True => Early Binding / False => Late Binding
    #If FSO_EarlyBind = True Then
        Dim oFSO              As Scripting.FileSystemObject
        Dim oFolder           As Scripting.folder

        Set oFSO = New FileSystemObject
    #Else
        Dim oFSO              As Object
        Dim oFolder           As Object

        Set oFSO = CreateObject("Scripting.FileSystemObject")
    #End If
    Dim sOutput               As String

    On Error GoTo Error_Handler

    If oFSO.FolderExists(sFolderPath) Then
        Set oFolder = oFSO.GetFolder(sFolderPath)

        If oFolder.Attributes And 1 Then sOutput = sOutput & "Read-Only" & sDelimiter    'FSO
        If oFolder.Attributes And 2 Then sOutput = sOutput & "Hidden" & sDelimiter    'FSO
        If oFolder.Attributes And 4 Then sOutput = sOutput & "System" & sDelimiter    'FSO
        If oFolder.Attributes And 8 Then sOutput = sOutput & "Volume" & sDelimiter    'FSO
        If oFolder.Attributes And 16 Then sOutput = sOutput & "Directory" & sDelimiter    'FSO
        If oFolder.Attributes And 32 Then sOutput = sOutput & "Archive" & sDelimiter    'FSO
        If oFolder.Attributes And 1024 Then sOutput = sOutput & "Alias" & sDelimiter    'FSO
        'If oFolder.attributes And 1024 Then sOutput = sOutput & "ReparsePoint" & sDelimiter 'File Attribute Const?
        If oFolder.Attributes And 2048 Then sOutput = sOutput & "Compressed" & sDelimiter    'FSO
        'If oFolder.Attributes And 8192 Then sOutput = sOutput & "NotContentIndexed" & sDelimiter    'File Attribute Const?
        'If oFolder.Attributes And 16384 Then sOutput = sOutput & "Encrypted" & sDelimiter    'File Attribute Const?
        'If oFolder.Attributes And 32768 Then sOutput = sOutput & "Integrity Stream" & sDelimiter    'File Attribute Const?
        'If oFolder.Attributes And 524288 Then sOutput = sOutput & "Pinned" & sDelimiter    'File Attribute Const?
        'If oFolder.Attributes And 1048576 Then sOutput = sOutput & "Unpinned" & sDelimiter    'File Attribute Const?

        If sOutput = "" Then
            sOutput = "Normal"
        Else
            sOutput = Left(sOutput, Len(sOutput) - Len(sDelimiter))    ' Remove the trailing delimiter
        End If
    Else
        sOutput = "'" & sFolderPath & "' does not exist"
    End If

    FSO_GetoFolderAttributes = sOutput

Error_Handler_Exit:
    On Error Resume Next
    Set oFolder = Nothing
    Set oFSO = Nothing
    Exit Function

Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
           "Error Source: FSO_GetoFolderAttributes" & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Description: " & Err.Description & _
           Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
           , vbOKOnly + vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Function

For more information on the subject be sure to check out the official documentation at:

6 responses on “Using the File System Object (FSO) for Retrieving Folder Attributes

  1. John Mallinson

    Hi Daniel, thanks for the post. I wanted to ask where the values you have commented as “‘File Attribute Const?” come from (they’re not part of the FSO … per the page you link to for the Attributes property values) and how they are used? For example, I mean how a folder becomes “Pinned” or “Unpinned” … I tried both “Pin to Start” and “Pin to Quick access” options within File Explorer but neither of these actions caused your Function to return “Pinned” or “Unpinned” for the relevant folder. Thanks again.

      1. John Mallinson

        Thanks Daniel. I wonder if those values can be returned by the FSO Folder.Attributes property? They don’t exist in the Microsoft Scripting Runtime library. They might only be available when the file/folder is queried via the GetFileInformationByHandleEx (Windows API) function?

  2. Jason Lee Hayes

    I use this code to determine if a folder is pinned or unpinned from the Quick Access

    Sub CheckIfFolderPinnedToQuickAccess(folderPath As String)
    Dim shell As Object
    Dim folder As Object
    Dim items As Object
    Dim item As Object
    Dim isPinned As Boolean

    ‘ Initialize the Shell application
    Set shell = CreateObject(“Shell.Application”)

    ‘ Get the Quick Access folder
    Set folder = shell.Namespace(“shell:::{679f85cb-0220-4080-b29b-5540cc05aab6}”)
    Set items = folder.items

    ‘ Initialize isPinned to False
    isPinned = False

    ‘ Loop through the items in Quick Access to check if the folder is pinned
    For Each item In items
    If item.path = folderPath Then
    isPinned = True
    Exit For
    End If
    Next item

    ‘ Display the result
    If isPinned Then
    MsgBox “The folder is pinned to Quick Access.”, vbInformation
    Else
    MsgBox “The folder is not pinned to Quick Access.”, vbInformation
    End If

    ‘ Clean up
    Set item = Nothing
    Set items = Nothing
    Set folder = Nothing
    Set shell = Nothing
    End Sub

    Sub Example()
    Call CheckIfFolderPinnedToQuickAccess(“C:\Users\YourUsername\Documents”)
    End Sub

  3. Jason Lee Hayes

    To Check if Pinned to Start:-

    Set folder = shell.Namespace(“shell:::{4234d49b-0245-4df3-b780-3893943456e1}”)