1. Exploring FileSystemObject (FSO) in VBA
2. Objects Accessible through FileSystemObject
3. Activating FileSystemObject in VBA
4. Examples of FileSystemObject in VBA
5. Methods of FileSystemObject (FSO) in VBA
1. Exploring FileSystemObject (FSO) in VBA
FileSystemObject (FSO) enables access to file systems on the computer. By utilizing FileSystemObject (FSO), we can access and modify files, directories on our computer system.
Below are some specific scenarios where we can employ FileSystemObject (FSO) in VBA:
- Check if a file or directory exists.
- Create or rename a directory/file.
- Retrieve a list of all file names (or subdirectory names) within a directory.
- Copy files from one directory to another.
In some of the mentioned cases above, we can also achieve through the use of traditional VBA functions (such as the DIR function) and methods, however, the code will be longer and more complex.
FileSystemObject facilitates easier handling of files and directories while significantly reducing the length and complexity of code.
Note: FSO can only be used in Excel 2000 and later versions.
2. Objects Accessible through FileSystemObject
As mentioned earlier, by using FileSystemObject in VBA, we can access and modify the names of files and directories.
Below is a table listing important objects that we can access and modify using FSO:
Object Description
Drive The Drive object allows us to retrieve information about drives, such as whether the drive exists or not, the path name, the type of drive (removable or fixed), size, etc.
Folder The Folder object allows for creating or editing directories in the system, such as deleting, creating, renaming, copying directories, ... .
File The File object enables working with files on the system, such as creating, opening, copying, moving, and deleting files, ... .
TextStream The TextStream object allows for creating or reading text files.
Each object will have methods that we can use in combination with each other.
For example, if we want to delete a directory, we use the DeleteFolder method of the Folder object. Similarly, if we want to copy a file, we use the CopyFile method of the File object.
3. Activating FileSystemObject in VBA
Check out how to Activate FileSystemObject in VBA here in detail.
4. Examples of FileSystemObject in VBA
Check out the details of Examples of FileSystemObject in VBA here.
5. Methods of FileSystemObject (FSO) in VBA
Below are the methods that we can use for each object of FileSystemObject (FSO) in VBA:
FSO Method Object Description
DriveExists Drive Checks whether the drive exists or not.
GetDrive Drive Returns the drive object based on the specified path.
GetDriveName Drive Returns the drive name.
BuildPath File/Folder Constructs a path from an existing path and a path name.
CopyFile File/Folder Copies the file.
GetAbsolutePathName File/Folder Returns the exact path.
GetBaseName File/Folder Returns the base name from the path, for example, D:\TestFolder\TestFile.xlsm will return TextFile.xlsm.
GetTempName File/Folder Generates a name that can be used to set for a temporary file.
CopyFolder File/Folder Copies the folder from one location to another.
CreateFolder Folder Creates a new folder.
DeleteFolder Folder Deletes a specific folder.
FolderExists Folder Checks whether the folder exists or not.
GetFolder Folder Returns a variant of the Folder object based on the specific path.
GetParentFolderName Folder Returns the name of the parent folder based on the specified path.
GetSpecialFolder Folder Retrieves the location of various system folders.
MoveFolder Folder Moves the folder from one location to another.
DeleteFile File Deletes a file.
FileExists File Checks whether the file exists or not.
GetExtensionName File Returns the file extension.
GetFile File Returns a variant of a File object based on the specified path.
GetFileName File Returns the file name.
GetFileVersion File Returns the file version.
MoveFile File Moves the file.
CreateTextFile File Creates a text file.
GetStandardStream File Accesses the standard input, output, or error stream.
OpenTextFile File Opens a file as a TextStream.
In the above article, readers have just explored FileSystemObject (FSO) in VBA with Mytour. If there are any questions or queries needing answers, readers can leave their comments below the article.
You can continue to explore FileSystemObject in VBA part 2 in our next article, which is highly important content in VBA programming.
