Return to VBA Code Examples

VBA GetFolder & GetFile (Get File & Folder Properties)

This tutorial will demonstrate how to use the GetFolder and GetFile methods of the FileSystemObject.

Get Folder and File Properties with VBA FileSystemObject

The GetFolder method returns a Folder object corresponding to the folder in a specified path and lets you access its properties. The GetFile method does the same with the file specified.

Set VBA Reference

First, when using FileSystemObjects, you may have to set a reference to the VB script run-time library: open the Visual Basic Editor (ALT+F11), select Tools > References from the drop-down menu and tick the check-box of ‘Microsoft Scripting Runtime’.

vba references window

FileSystemObject

Second, you must create the FileSystemObject:

Now you have access to GetFolder, and the other FileSystemObject Methods.

Use of GetFolder Method

After specifying the folder you want to access

you can copy it:

move it:

delete it:

or create a new text file in it:

By using this method, you get access to the folder’s properties such as its attributes (fld.Attributes), the date and time when it was created (fld.DateCreated), last accessed (fld.DateLastAccessed), last modified (fld.DateLastModified), its drive’s letter (fld.Drive), its name and short name (fld.Name, fld.ShortName), its path and short path (fld.Path, fld.ShortPath), its size (fld.Size), its type (fld.Type), its parent folder (fld.ParentFolder), check whether it is a root folder (fld.IsRootFolder) or you can loop through, count, etc. its files (fld.Files) or subfolders (fld.SubFolders).

Putting this all together in a procedure would look like this:

Please note that you shall press Ctrl+G in order to see the result of Debug.Print command in the VBA immediate window.

GetParentFolderName Method

Alternatively to the above mentioned way, you can access a folder’s parent folder’s name by using this code:

ParentFold will be in this case “C:\ParentTest\”.

Please note that this method will not resolve the path, nor does it check for the existence of the specified path.

GetSpecialFolder Method

With the GetSpecialFolder method, by passing 0, 1 or 2 as an argument, you can get your Windows folder path (with files installed by the Windows operating system), your system folder path (with libraries, fonts, and device drivers) and temporary folder path (the folder that is used for storing temporary files), respectively.

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro - A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users! automacro

Learn More!!

GetFile Method

You can use the GetFile method in a very similar way as the GetFolder method. After specifying the file you want to access

you can copy it:

move it:

delete it:

or open it as a TextStream object:

The file’s properties such as its attributes, the date and time when it was created, last accessed or last modified, its drive’s letter, name and short name, path and short path, size, type and its parent folder can be accessed the same way as described at the GetFolder method.