Skip to main content

Files collection

Table of contents
  1. Examples

Collection of all File objects within a folder.

Examples

The following code illustrates how to get a Files collection and iterate the collection by using the For Each...Next statement.

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

Run this code in Immediate Window:

Debug.Print ShowFolderList("D:\TestFolder")

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>