[Excel VBA] How to Get File and Folder Information Using FSO (GetFile, GetFolder)

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

PropertyDescription
.NameFile name (including extension)
.PathFull path of the file
.ParentFolderPath of the parent folder
.SizeFile size (in bytes)
.TypeFile type (e.g., “Microsoft Excel Worksheet”)
.DateCreatedDate and time created
.DateLastModifiedDate and time last modified
.DateLastAccessedDate and time last accessed

Main Properties of the Folder Object

PropertyDescription
.NameFolder name
.PathFull path of the folder
.ParentFolderPath of the parent folder
.SizeTotal size of all items in the folder (in bytes)
.FilesCollection of all File objects in the folder
.SubFoldersCollection of all Folder objects (subfolders) in the folder
.DateCreatedDate 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.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次