Skip to main content

Folder.Files property

Table of contents
  1. Syntax
  2. Examples

Returns a Files collection consisting of all File objects contained in the specified folder, including those with hidden and system file attributes set.

Syntax

object.Files

The object is always a Folder object.

Examples

The following code illustrates the use of the Files property.

Sub ShowFileList(folderspec)
    Dim fso, f, f1, fc, s
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.GetFolder(folderspec)
    Set fc = f.Files
    For Each f1 In fc
        s = s & f1.Name
        s = s & vbCrLf
    Next
    Debug.Print s
End Sub

Run the following code in the Immediate Window to test this macro:

ShowFileList "D:\TestFolder"

This macro lists all files of TestFolder folder (exclude sub folder), if you want to list all files (include sub folders), read this: Excel Macro: List All Files in Folders and Subfolders

Result:

hidden.txt
hidden_readonly.txt
readonly.txt

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>