Tags

, ,

In this article I will explain how you can  find and create a list of all the files and folder in a directory:

Jump To:

Get List of All Files Using, FileSystemObject:

In this example I will assume that I have the following files in the directory “D:\Stuff\Freelances\Website\Blog\Arrays\Pics”:

Files In Directory

The code below retrieves the file in this directory and creates a list of their names and paths:

Sub Example1()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer

'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder("D:\Stuff\Freelances\Website\Blog\Arrays\Pics")
i = 1
'loops through each file in the directory and prints their names and path
For Each objFile In objFolder.Files
    'print file name
    Cells(i + 1, 1) = objFile.Name
    'print file path
    Cells(i + 1, 2) = objFile.Path
    i = i + 1
Next objFile
End Sub

Result:
File Names and Path Excel VBA

Get List of All Folders Using, FileSystemObject:

In this example I will use the directory “D:\Stuff\Freelances\Website\Blog”. The following folders can be found in that directory:

Folders in Path

Using the code below, the names of the folders and their associated paths are listed on column A and B:

Sub Example2()
Dim objFSO As Object
Dim objFolder As Object
Dim objSubFolder As Object
Dim i As Integer

'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder("D:\Stuff\Freelances\Website\Blog")
i = 1
'loops through each file in the directory and prints their names and path
For Each objSubFolder In objFolder.subfolders
    'print folder name
    Cells(i + 1, 1) = objSubFolder.Name
    'print folder path
    Cells(i + 1, 2) = objSubFolder.Path
    i = i + 1
Next objSubFolder
End Sub

Result:
List of Folder Names and Paths Excel VBA

Dir():

Using the Dir() function you can get the list of files and folders in a specific path. The Dir() function takes 2 input parameters, the directory path and the type of file we are looking for:

strFileOrFolder = Dir(strPath, FileAttribute)

strPath is the path of the directory which the files and folder are in.  The FileAttribute specifies what type of file or folder we are after. For more information about the FileAttribute parameter click here.

Get List of All Files in a Directory Using Dir():

In this example I will assume that I have the following files in the directory “D:\Stuff\Freelances\Website\Blog\Arrays\Pics”:

Files In Directory

The code below retrieves the name and path of the files  in that directory using the DIR() function and lists them in column A and B:

Sub Example3()

Dim varDirectory As Variant
Dim flag As Boolean
Dim i As Integer
Dim strDirectory As String

strDirectory = "D:\Stuff\Freelances\Website\Blog\Arrays\Pics\"
i = 1
flag = True
varDirectory = Dir(strDirectory, vbNormal)

While flag = True
    If varDirectory = "" Then
        flag = False
    Else
        Cells(i + 1, 1) = varDirectory
        Cells(i + 1, 2) = strDirectory + varDirectory
        'returns the next file or directory in the path
        varDirectory = Dir
        i = i + 1
    End If
Wend

Result:
File Names and Path Excel VBA

Get List of All Folders and Files in a Directory Using Dir():

In this example I will assume that I have the following files and  folders in the directory “D:\Stuff\Freelances\Website\Blog”:

Folders in Path

The code below retrieves the name and path of the folders and files in that directory using the DIR() function and lists them in column A and B:

Sub Example4()

Dim varDirectory As Variant
Dim flag As Boolean
Dim i As Integer
Dim strDirectory As String

strDirectory = "D:\Stuff\Freelances\Website\Blog\"
i = 1
flag = True
varDirectory = Dir(strDirectory, vbDirectory)

While flag = True
    If varDirectory = "" Then
        flag = False
    Else
        Cells(i + 1, 1) = varDirectory
        Cells(i + 1, 2) = strDirectory + varDirectory
        'returns the next file or directory in the path
        varDirectory = Dir
        i = i + 1
    End If
Wend

Result:
List of Folders and File Names and Paths DIR Excel VBA

See Also:

If you need assistance with your code, or you are looking to hire a VBA programmer feel free to contact me. Also please visit my website  www.software-solutions-online.com

Hello zapp10anth, thank you for visiting my website.