2014-01-23



Introduction

The File System Object (FSO) provides access to a computer’s file system. The particular object contains 3 object collections, 4 other objects, as well as several properties and methods (see the picture below). FSO is a quite powerful object, but it should be used with caution; since it can manipulate files that might belong to the operating system, a poorly written VBA code can even cause malfunctions in the entire computer. I would suggest you to take a backup of your folder/file that you need to manipulate (copy, move, delete etc.) before running any code. In any case, be careful when you working with FSO, and, especially, when you deleting files.



The section below focuses on three useful properties of the File System Object – Drive Exists, Folder Exists and File Exists – which can be used to determine the existence of a drive, folder and file respectively. I have adapted these properties into three corresponding VBA functions, which return string messages depending on whether or not the specified drive/folder/file exist.

VBA code

All function share the same approach; create the FSO object and then apply the corresponding property. The VBA code of the DriveExists function is presented first.

The FolderExists function follows:

Finally, the FileExists function is given below.

The above functions were demonstrated in order to give you some insights about the FSO usage. It should be highlighted, that instead of these functions, a much simpler function can be developed, which will use the Dir function. The Dir function returns a string representing the name of a file, directory, or folder that matches a specified pattern or file attribute, or the volume label of a drive. The PathExists function that follows implements the Dir function to determine the drive, folder and file existence.

Two different ways - one specific and one generic - were presented for determining if a drive/folder/file exists. You can choose the function that best fits to your needs.

Downloads



The file can be opened with Excel 2007 or newer. Please enable macros before using it.

FSO related applications

The list below contains various applications that I have developed and were based on the objects, properties and method of the File System Object. You can download them for free:

Folders, Subfolders & Files

Move Files To Subfolders

Read MP3 Tags

List All Your VBA Procedures

Organize Your Office Files

Quick Back Up Of Your Files

Show more