VBA FileSystemObject (FSO)

Publication Date :

Blog Author :

Download FREE VBA FileSystemObject (FSO) In Excel Template and Follow Along!
VBA FileSystemObject Excel Template.xlsm

Table Of Contents

arrow

Excel VBA FileSystemObject (FSO)

VBA FileSystemObject(FSO) works similar to FileDialog, used to get access to other files of the computer we are working on. We can also edit these files to read or write the file. Using FSO, we can access files, work with them, and modify files and folders. FSO is the important API tool we can access with VBA. As part of the VBA project, we may need to access a few folders and files on our computer to complete the job.

We can do many tasks by using FSO, like checking whether the folder is available, creating a new folder or files, renaming the existing folder or files, getting the list of all the files in the folder, and subfolder names. Finally, we can copy files from one location to another.

Even though other functions are available to work with folders and files, FSO is the easiest method to work with folders and files by keeping the VBA code neat and straight.

We can access four types of objects with FileSystemObject. Below are those.

  1. Drive: Using this object, we can check whether the mentioned drive exists or not. We can get the pathname, type of purpose, and size of the enterprise.
  2. Folder: This object allows us to check whether the particular folder exists or not. We can create, delete, modify, and copy folders using this object.
  3. File: This object allows us to check whether the particular file exists or not. We can create, delete, modify, and copy files using this VBA object.
  4. Text Stream: This object allows us to create or read text files.

All the above methods have their method to work with. Based on our requirements, we can choose the method for each object.

VBA FileSystemObject (FSO)
You are free to use this image on your website, templates, etc.. Please provide us with an attribution link.

How to Enable FileSystemObject?

It is not readily accessible in VBA. Since accessing files and folders is the outside task of Excel, we need to enable the FileSystemObject.

To encourage, follow the below steps.

  1. Go to Tools > References.


    Activate File System object step 1

  2. Select the "Microsoft Scripting Runtime" option. Next, scroll down and select the "Microsoft Scripting Runtime" option. After choosing the options, click on "OK."


    Activate File System object step 2

    Now, we can access the FileSystemObject (FSO) in VBA.

  3. Create an Instance of FileSystemObject


    Once we enable the "Microsoft Scripting Runtime" option from the Objects library, we need to create a FileSystemObject (FSO) through coding.

  4. To create the instance, first declare the variable as FileSystemObject.


    Instance File system Object 1

  5. As we can see, FileSystemObject appears in the IntelliSense list in VBA. Therefore, it wouldn't have been available before we enabled the 'Microsoft Scripting Runtime.' Since FSO is an object, we need to set it to create a new instance.


    Instance File system Object 1-1

  6. Now, we can access all the options of FSO(FileSystemObject).


    Instance File system Object 1-2

Examples to use VBA FileSystemObject

Example #1 - Find the Total Drive Space

The below code will give the total space of the drive.

Code:

Sub FSO_Example1()

    Dim MyFirstFSO As FileSystemObject
    Set MyFirstFSO = New FileSystemObject

    Dim DriveName As Drive
    Dim DriveSpace As Double

    Set DriveName = MyFirstFSO.GetDrive("C:")
    'Create new drive object

    DriveSpace = DriveName.FreeSpace
    'This will get the free space of the drive "C"

    DriveSpace = DriveSpace / 1073741824
    'This will convert the free space to GB

    DriveSpace = Round(DriveSpace, 2)
    'Round the total space

    MsgBox "Drive " & DriveName & " has " & DriveSpace & "GB"

End Sub
VBA FileSystemObject Example 1

Break down the code.

First, we created an instance of FSO.

Dim MyFirstFSO As FileSystemObject
Set MyFirstFSO = New FileSystemObject

Next, we have declared two variables.

Dim DriveName As Drive
Dim DriveSpace As Double

Since DriveName is an Object variable, we need to set this to FSO, one of the FSO methods. Since we need the drive's characteristics, we have used the “GetDrive” option and mentioned the drive name.

Set DriveName = MyFirstFSO.GetDrive("C:")

For another variable, DriveSpace, we will assign the free space method of the drive we are accessing.

DriveSpace = DriveName.FreeSpace

Currently, the above equation can get us free space of the drive "C." So to show the result in GB, we have divided the open space by 1073741824.

DriveSpace = DriveSpace / 1073741824

Next, we will round the number.

DriveSpace = Round(DriveSpace, 2)

Finally, show the result in Message Box.

MsgBox "Drive " & DriveName & " has " & DriveSpace & "GB"

When we run the code manually or through shortcut key F5, then in the message box, we will get the free space of the drive "C."

VBA FileSystemObject Example 1-1

So, our computer Drive C has 216.19 GB of free space memory.

Example #2 - Check Whether the Folder Exists or Not

To check whether the particular folder exists, use the code below.

If the mentioned folder is available, it will show the message box as "The Mentioned Folder is Available." If not, it will show the VBA message box as "The Mentioned Folder is Not Available."

Code:

Sub FSO_Example2()

    Dim MyFirstFSO As FileSystemObject
    Set MyFirstFSO = New FileSystemObject

    If MyFirstFSO.FolderExists("D:Excel FilesVBAVBA Files") Then
    MsgBox "The Mentioned Folder is Available"
    Else
    MsgBox "The Mentioned Folder is Not Available"
    End If

End Sub
VBA FileSystemObject Example 2

Run this code through the excel Shortcut key F5 or manually, then see the result.

VBA FileSystemObject Example 2-1

Example #3 - Check Whether the File Exists or Not

The below code will check whether the mentioned file is available or not.

Code:

Sub FSO_Example3()

   Dim MyFirstFSO As FileSystemObject
   Set MyFirstFSO = New FileSystemObject

   If MyFirstFSO.FileExists("D:Excel FilesVBAVBA FilesTesting File.xlsm") Then
   MsgBox "The Mentioned File is Available"
   Else
   MsgBox "The Mentioned File is Not Available"
   End If

End Sub
VBA FileSystemObject Example 3

Run this code manually or use the F5 key, then see the result.

VBA FileSystemObject Example 3-1