In general when we write VBA scripts we either trying to automate an office application task like in Excel, Outlook, Word, or Access, or we are trying to implement a functionality to an existing program. One thing makes VBA versatile is its ability to work with Windows System API.
One of my favorite Window System API is the FileSystemObject (FSO).
The FileSystemObject gives you the ability to access a computer’s file system, that means you can manipulate files, folders, directory paths, as long as you have sufficient permission and privilege. If you are often having the need to organize files or folders, by knowing how to work with FileSystemObject, the skill will help you with your files and folders management tremendously.
The FileSystemObject has been around for a long long time, but even today, it is still pretty widely used on Windows. In this tutorial we will learn how to use the FileSystemObject’s methods by going through different examples together.
Example 1. BuildPath
Appends a name to an existing folder path.
Syntax
BuildPath(path, name)
Example 2. CopyFile
Copies one or more files from one location to another.
Syntax
CopyFile(source file path, destination folder path, [overwrite])
Example 3. CopyFolder
Copies one or more folders from one location to another.
Syntax
CopyFolder(source folder path, destination folder path, [overwrite])
Example 4. CreateFolder
Creates a new folder.
Syntax
CreateFolder(folder name)
Example 5. CreateTextFile
Creates a text file and returns as a TextStream object.
Syntax
CreateTextFile(file name, [overwrite], [unicode])
Example 6. DeleteFile
Deletes one or more files.
Syntax
DeleteFile(file path, [force])
Example 7. DeleteFolder
Deletes one or more folders.
Syntax
DeleteFolder(folder path, [force])
Example 8. DriveExists
Check if a specified drive exists.
Syntax
DriveExists(drive spec)
Example 9. FileExists
Check if a specified file exists.
Syntax
FileExists(file path)
Example 10. FolderExists
Check if a specified folder exists.
Syntax
FolderExists(folder path)
Example 11. GetAbsolutePathName
Returns the complete path from the root of the drive for the specified path.
Syntax
GetAbsolutePathName(path spec)
Example 12. GetBaseName
Returns the base name of a specified file or folder.
Syntax
GetBaseName(file or folder path)
Example 13. GetDriveName
Returns the drive name giving a specified file or folder path.
Syntax
GetDriveName(file or folder path)
Example 14. GetDrive
Returns a Drive object corresponding to the drive in a specified path.
Syntax
GetDrive(drive spec)
Example 15. GetExtensionName
Returns the file extension name.
Syntax
GetExtensionName(file path)
Example 16. GetFile
Returns a File object giving a specified file path.
Syntax
GetFile(file path)
Example 17. GetFileName
Returns the file name or folder name of the last component in a specified path.
Syntax
GetFileName(file path)
Example 18. GetFolder
Returns a Folder object giving a specified path.
Syntax
GetFolder(folder path)
Example 19. GetParentFolderName
Returns the name of the parent folder of the last component in a specified file or folder path.
Syntax
GetParentFolderName(folder name)
Example 20. GetSpecialFolder
Returns the Windows System paths
Syntax
GetSpecialFolder(folder spec)
Example 21. GetTempName
Returns a randomly generated temporary file name.
Syntax
GetTempName()
Example 22. MoveFile
Move one or more files from one location to another.
Syntax
MoveFile(source file path, destination folder path)
Example 23. MoveFolder
Move one or more folders from one location to another.
Syntax
MoveFolder(source folder path, destination folder path)
Example 24. OpenTextFile
Opens a file as a TextStream object.
Syntax
OpenTextFile(file name, [io mode], [create], [format]
io mode settings
[table id=3 /]
format settings
[table id=4 /]