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 means read or write the file. Using FSO we can access files, work with them, 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 in our computer to get the job done.
We can do many tasks by using FSO like “to check whether the folder is available or not”, Create new folder or files, rename the existing folder or files, get the list of all the files in the folder and also subfolder names, and finally, we can copy files from one location to another.
Even there are other functions 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 4 types of Objects with FileSystemObject. Below are those.
- Drive: Using this object we can check whether the mentioned drive exists or not, we can get the pathname, type of drive, and size of the drive.
- Folder: This object allows us to check whether the particular folder exists or not. We can create, delete, modify, copy folders using this object.
- File: This object allows us to check whether the particular file exists or not. We can create, delete, modify, copy files using this vba object.
- Text Stream: This object allows us to create or read text files.
All the above methods have their own method to work with. Based on our requirement we can choose the method of each object.
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 enable follow below steps.
Step #1: Go to Tools>References.
Step #2 – Select ‘Microsoft Scripting Runtime’ option
Scroll down and select ‘Microsoft Scripting Runtime’ option. After selecting the options click on OK.
Now we can access the FileSystemObject (FSO) in vba.
Create an Instance of FileSystemObject
Once the ‘Microsoft Scripting Runtime’ option is enabled from Objects library, we need to create the instance of File System Object (FSO) through coding.
To create the instance first declare the variable as FileSystemObject.
As we can see FileSystemObject is appearing in the IntelliSense list in VBA. This wouldn’t have available before we enable the ‘Microsoft Scripting Runtime’.
Since FSO is an object we need to set it to create a new instance.
Now we can access all the options of FSO(FileSystemObject).
Examples to use VBA FileSystemObject
Example #1 – Find the Total Drive Space
Below code will give the total space of the drive.
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
Break Down of 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 method. Since we need the characteristic of the drive we have used Get Drive option and mentioned the drive name
Set DriveName = MyFirstFSO.GetDrive("C:")
Now for another variable DriveSpace, we will assign the free space method of the drive we are accessing.
DriveSpace = DriveName.FreeSpace
As of now, the above equation can get us free space of the drive “C”. So to show the result in GB we have divided the free 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 message box we will get the free space of the drive “C”.
So, in my 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 or not use the below code.
If the mentioned folder is available then it will show us 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”.
Sub FSO_Example2() Dim MyFirstFSO As FileSystemObject Set MyFirstFSO = New FileSystemObject If MyFirstFSO.FolderExists("D:\Excel Files\VBA\VBA Files") Then MsgBox "The Mentioned Folder is Available" Else MsgBox "The Mentioned Folder is Not Available" End If End Sub
Run this code through the excel Shortcut key F5 or manually, then see the result.
Example #3 – Check Whether the File Exists or Not
Below code will check whether the mentioned file is available or not.
Sub FSO_Example3() Dim MyFirstFSO As FileSystemObject Set MyFirstFSO = New FileSystemObject If MyFirstFSO.FileExists("D:\Excel Files\VBA\VBA Files\Testing File.xlsm") Then MsgBox "The Mentioned File is Available" Else MsgBox "The Mentioned File is Not Available" End If End Sub
Run this code manually or using the F5 key, then see the result.
This has been a guide to VBA FileSystemObject. Here we learn how to use FileSystemObject (FSO) in VBA to access files and folders from the computer along with practical examples and downloadable excel template. Below you can find some useful excel VBA articles –