Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
551 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Written by Andy Brown
In this tutorial
You won't care what a FileSystemObject is, but you will care what it can do for you. You can use FileSystemObjects to:
get access to the files and folders on your hard disk (to move, copy, rename, delete or open them); and
create and open TextStreams to read text files in line by line, or to write to them.
Each of these topics is described in separate tutorials - the only purpose of this tutorial is to show how to start you off by creating a FileSystemObject. You'll need to do two things:
Create a reference to the Microsoft Scripting Runtime object library.
Create a new FileSystemObject variable.
Here's how to do this!
The easiest way to think of a FileSystemObject is as a running copy of File Explorer. The only difference between referencing a FileSystemObject and referencing an application like Word is that you can't make the FileSystemObject visible (all the file and folder manipulation is done in the background).
This is hard to describe, but easy to do. First, you need to choose to create a reference within your Excel workbook (or Word document, PowerPoint presentation or Access database):
Select the menu option shown above.
Within VBA, select Tools References from the menu as shown above.
Tick the Microsoft Scripting Runtime library as shown below, and click OK.
Select the object library shown above.
If you now select Tools -> References from the menu again, you should see the option you selected near the top:
The object libraries that you are referencing appear near the top of the list.
Now that you've referenced this library, it's time to create a FileSystemObject.
The easiest way to do this is to create and instantiate a public variable to refer to a FileSystemObject in a single line.
If that sentence didn't make much sense to you, don't worry! In practice all that you need to do is to type in a single line of code, and everything else will fall into place!
Here's a line of code to do this:
The only reason FileSystemObject appears in the list is because you have referenced the object library containing it.
There is much more on FileDialogs (and on reading/writing to/from text files) in separate tutorials, but here's some sample code to give a flavour:
'ensures you have to declare all variables used
Option Explicit
'create a public variable to refer
'to a file system object
Public fso As New FileSystemObject
Sub ListFiles()
'some example code to list out all
'of the files in a given folder
Dim fol As Folder
Dim fil As File
Set fol = fso.GetFolder("C:\wise owl\")
For Each fil In fol.Files
Debug.Print fil.Name
Next fil
End Sub
Running the ListFiles routine shown above would create a listing of all the files in the wise owl folder of your C drive in the immediate window.
You can learn more about this topic on the following Wise Owl courses:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.