The basis of file manipulation using the FileSystemObject (FSO) starts with getting the target file or folder as an “Object.”
By retrieving it as an object, you can easily access various information held by that file or folder, such as its name, path, size, and creation date.
In this article, I will explain the basic method of retrieving file and folder information using the FSO GetFile and GetFolder methods.
Preparation: Reference Settings
To use FSO comfortably, I recommend enabling the reference. In the VBA Editor, go to Tools > References and check “Microsoft Scripting Runtime”.
The Completed VBA Code
Below is the VBA code that retrieves information about a specified file and folder and displays some of that content in a message box.
' Reference: Microsoft Scripting Runtime
Sub GetFileAndFolderInfo()
' Declare variables
Dim fso As New FileSystemObject
Dim targetFile As File
Dim targetFolder As Folder
Dim message As String
'--- Settings ---
Dim filePath As String
Dim folderPath As String
filePath = ThisWorkbook.FullName ' Target this Excel file itself
folderPath = ThisWorkbook.Path ' Target the folder containing this file
'--- End Settings ---
'--- 1. Get File and Folder Objects ---
' Check if the file exists first
If Not fso.FileExists(filePath) Then
MsgBox "The specified file was not found.", vbCritical
Exit Sub
End If
Set targetFile = fso.GetFile(filePath)
' Check if the folder exists first
If Not fso.FolderExists(folderPath) Then
MsgBox "The specified folder was not found.", vbCritical
Exit Sub
End If
Set targetFolder = fso.GetFolder(folderPath)
'--- 2. Read information from Object properties ---
message = "[File Info]" & vbCrLf & _
"File Name: " & targetFile.Name & vbCrLf & _
"Full Path: " & targetFile.Path & vbCrLf & _
"Size: " & Format(targetFile.Size, "#,##0") & " bytes" & vbCrLf & _
"Created: " & targetFile.DateCreated & vbCrLf & _
"--------------------" & vbCrLf & _
"[Folder Info]" & vbCrLf & _
"Folder Name: " & targetFolder.Name & vbCrLf & _
"Parent Folder: " & targetFolder.ParentFolder & vbCrLf & _
"File Count: " & targetFolder.Files.Count
'--- 3. Display Results ---
MsgBox message, vbInformation, "File and Folder Info"
' Release objects
Set targetFile = Nothing
Set targetFolder = Nothing
Set fso = Nothing
End Sub
Explanation of Key Points
Getting a File Object: .GetFile
Set myFile = fso.GetFile("Full Path to File")
The GetFile method returns the file specified by the argument path as a File Object. It is safer to check for the file’s existence using fso.FileExists("Path") before attempting to get the object.
Getting a Folder Object: .GetFolder
Set myFolder = fso.GetFolder("Path to Folder")
The GetFolder method returns the folder specified by the path as a Folder Object. Similarly, it is standard practice to check for existence using fso.FolderExists("Path") first.
List of Main Properties
Once you obtain a File or Folder object, you can access a variety of useful properties.
Main Properties of the File Object
| Property | Description |
| .Name | File name (including extension) |
| .Path | Full path of the file |
| .ParentFolder | Path of the parent folder |
| .Size | File size (in bytes) |
| .Type | File type (e.g., “Microsoft Excel Worksheet”) |
| .DateCreated | Date and time created |
| .DateLastModified | Date and time last modified |
| .DateLastAccessed | Date and time last accessed |
Main Properties of the Folder Object
| Property | Description |
| .Name | Folder name |
| .Path | Full path of the folder |
| .ParentFolder | Path of the parent folder |
| .Size | Total size of all items in the folder (in bytes) |
| .Files | Collection of all File objects in the folder |
| .SubFolders | Collection of all Folder objects (subfolders) in the folder |
| .DateCreated | Date and time created |
Summary
File manipulation using FSO starts with retrieving the target as an object.
- For Files:
fso.GetFile("Path") - For Folders:
fso.GetFolder("Path")
Once you have the object, you can easily and intuitively retrieve detailed information about the file or folder by simply referencing properties like .Name or .Size.
