This also means that most of the time, we work with cells and ranges, worksheets, workbooks, and other objects which are a part of the Excel application. But VBA is a lot more powerful and can also be used to work with stuff outside of Excel. In this tutorial, I will show you how to use VBA FileSystemObject (FSO) to work with files and folders on your system or network drives.
What is VBA FileSystemObject (FSO)?
FileSystemObject (FSO) allows you to access the file system of your computer. Using it, you can access and modify the files/folders/directories in your computer system. For example, below are some of the things you can do by using FileSystemObject in Excel VBA:
Check if a file or a folder exists. Create or rename folders/files. Get a list of all the file names (or sub-folder names) in a folder. Copy files from one folder to another.
I hope you get the idea. I will cover all these above examples (plus more) later in this tutorial. While some of the things mentioned above can also be done using traditional VBA functions (such as the DIR function) and methods, that would lead to longer and more complicated codes. FileSystemObject makes it easy to work with files and folders while keeping the code clean and short.
What All Objects Can You Access Through FileSystemObject?
As I mentioned above, you can access and modify files and folders using the FileSystemObject in VBA. Below is a table that shows the most important objects that you can access and modify using FSO: Each of the above objects has methods that you can use to work with these. To give you an example, if you want to delete a folder, you will use the DeleteFolder method of the Folder object. Similarly, if you want to copy a file, you will use the CopyFile method of the File object. Don’t worry if this seems overwhelming or hard to understand. You will get a much better understanding when you go through the examples that I have covered in this tutorial. Just for the reference purpose, I have covered all the FileSystemObject methods (for each object) at the end of this tutorial.
Enabling FileSystemObject in Excel VBA
FileSystemObject is not available by default in the Excel VBA. Since we are dealing with files and folders that are outside of the Excel application, we need to first create a reference to the library that holds these objects (drives, files, folders). Now there are two ways you can start using FileSystemObject in Excel VBA: While both these methods work (and I’ll show you how to do this next), I recommend using the first method.
Setting the Reference to the Microsoft Scripting Runtime Library
When you create a reference to the Scripting Runtime Library, you allow Excel VBA the access to all the properties and methods of files and folder. Once this is done, you can refer to the files/folders/drives object from within the Excel VBA (just like you can refer the cells, worksheets or workbooks). Below are the steps to create a reference to the Microsoft Scripting Runtime Library: The above steps would now allow you to refer to the FSO objects from Excel VBA.
Creating an Instance of FileSystemObject in the Code
Once you have set the reference to the Scripting FileSystemObject library, you need to create an instance of the FSO object in your code. Once this is created, you can use it in VBA. Below is the code that will set the object variable MyFSO as a FileSystemObject object: In this code, first I have declared the variable MyFSO as a FileSystemObject type object. This is possible only because I have created a reference to the Microsoft Scripting Runtime Library. If the reference is not created, this is going to give you an error (as Excel wouldn’t recognize what FileSystemObject means). In the second line, two things happen: If you want, you can also combine the above two statements into one as shown below: A big benefit of using this method (which is to set the reference to the Microsoft Scripting Runtime Library) is that when you use the FSO objects in your code, you will be able to use the IntelliSense feature that shows the methods and properties associated with an object (as shown below).
This is not possible when you create the reference from within the code (covered next).
Creating an Object from the Code
Another way to create a reference to FSO is by doing it from the code. In this method, you don’t need to create any reference (as done in the previous method). When you are writing the code, you can create an object from within the code and refer to the Scripting.FileSystemObject. The below code creates an object FSO and then makes this a FileSystemObject type. While this may seem more convenient, a big downside of using this method is that it would not show an IntelliSense when you work with objects in FSO. For me, this is a huge negative and I always recommend using the previous method of enabling FSO (which is by setting the reference to the ‘Microsoft Scripting Runtime’)
VBA FileSystemObject Examples
Now let’s dive in and have a look at some practical examples of using FileSystemObject in Excel.
Example 1: Check if a File or Folder Exists
The following code will check whether the folder with the name ‘Test’ exists or not (in the specified location). If the folder exists, the IF condition is True and it shows a message – ‘The Folder Exists’ in a message box. And if it doesn’t exist, it shows a message – The Folder Does Not Exist’. Similarly, you can also check if a file exists or not. The below code checks whether there is a file with the name Test.xlsx in the specified folder or not.
Example 2: Create a New Folder in the Specified Location
The below code would create a folder with the name ‘Test’ in the C drive of my system (you will have to specify the path on your system where you want to create the folder). While this code works fine, it would show an error in case the folder already exists. The below code checks whether the folder already exists and creates a folder if it doesn’t. In case the folder already exists, it shows a message. To check whether the folder exists, I have used the FolderExists method of the FSO.
Example 3: Get a List of All Files in a Folder
The below code would show the names of all the files in the specified folder. This code is a little more complex that the ones we have already seen. As I mentioned above in this tutorial, when you reference the ‘Microsoft Scripting Runtime Library’, you can use FileSystemObject as well as all other objects (such as Files and Folders). In the above code, I use three objects – FileSystemObject, File, and Folder. This allows me to go through each file in the specified folder. I then use the name property to get the list of all file names. Note that I am using Debug.Print to get the names of all the files. These names will be listed in the immediate window in the VB Editor.
Example 4: Get the List of All Sub-folders in a Folder
The below code will give the names of all the sub-folders in the specified folder. The logic is exactly the same as covered in the above example. Instead of files, in this code, we have used sub-folders.
Example 5: Copy a File from One Place to Another
The below code will copy the file from ‘Source’ folder and copy it to the ‘Destination’ folder. In the above code, I have used two variables – SourceFile and DestinationFolder. Source File holds the address of the file I want to copy and the DestinationFolder variable holds the address to the folder I want the file to be copied to. Note that it’s not sufficient to give the destination folder name when you’re copying a file. You also need to specify the file name. You can use the same file name or can also change it. In the above example, I copied the file and named it SampleFileCopy.xlsx
Example 6: Copy All Files From One Folder to Another
The below code will copy all the files from the Source folder to the destination folder. The above code will copy all the files from the Source folder to the Destination Folder. Note that in the MyFSO.CopyFile method, I have specified the ‘Overwritefiles’ property to be False (this is True by default). This makes sure that in case you already have the file in the folder, it’s not copied (and you will see an error). If you remove ‘Overwritefiles’ or set this to True, in case there are files in the destination folder with the same name, these would be overwritten. If you want to copy the files of a certain extension only, you can do that by using an IF Then statement to check whether the extension is xlsx or not.
FileSystemObject (FSO) Methods
Here are the methods that you can use for each object. This is just for reference purpose and doesn’t worry about it too much. The usage of some of these has been shown in the examples covered above.
Get a List of File Names from Folders & Sub-folders (using Power Query). Get the List of File Names from a Folder in Excel (with and without VBA). Understanding Excel VBA Data Types (Variables and Constants). Creating a User Defined Function (UDF) in Excel VBA.
Thanks for the information, however, I run into an issue with below, can you please help where am doing wrong. Path = “C:WSDataFilesPTP” & Cells(1, 2).Value Dim MyFSO As New FileSystemObject If MyFSO.FolderExists(Path) Then ‘Folder Exists Else MyFSO.CreateFolder (Path) ‘I tried this command also MyFSO.CreateFolder Path End If Regards Chandrakanth Jampala a = Cells(1, 2).Value path = “D:other” & a Sub GetFileNamesInSubFolders (Folder As Folder) Dim MyFSO As FileSystemObject Dim MyFile As File Dim MySubFolder As Folder If Folder Is Nothing Then Exit Sub For Each MySubFolder In Folder.SubFolders Debug.Print MySubFolder.Path For Each MyFile In MySubFolder.Files Debug.Print MyFile.Name Next MyFile Next MySubFolder ‘recursive call GetFileNamesInSubFolders MySubFolder End Sub It can be called from your procedure in example 3 with the Line… GetFileNamesInSubFolders MyFolder .. inserted just before ‘End Sub’. Btw: Your example 4 could also be complemented in this manner to list all subfolders in all subfolders by omitting the Lines ‘For Each Myfile ..’ to ‘… Next My File’ . Hope I did it correctly – in my test it works fine. Best regards Lothar Many thanks for sharing this Great and systematic tutorial. Just wanted to know on how to get a List of File Names from Folders & Sub-folders in Excel with VBA and create hyperlink to that file from that list . Looking forward to having your further advice in this regards Best regards Arsil Hadjar