FileSystemObject component
The FileSystemObject gives you access to the file system. It allows creating, manipulating, deleting, and obtaining information about drives, folders, and files. To use FileSystemObject in your VB code, you need to declare it in the following way.
Dim fso As New FileSystemObject
You can then use its properties and methods, which are shown in Table A.
Table A
|
FileSystemObject object model
Table B outlines the most often used methods of the FileSystemObject.
Table B
| Method |
Description |
Example |
| CreateFolder |
Creates a new folder |
fso.CreateFolder "C:\Files" |
| CreateTextFile |
Creates a specified filename and returns a TextStream object that can be used to read from or write to the file |
fso.CreateTextFile "C:\Files\file1.txt" |
| DeleteFolder |
Deletes folder |
fso.DeleteFolder "C:\Files" |
| DeleteFile |
Deletes file |
fso.DeleteFile "C:\Files.file.txt" |
| CopyFolder |
Copies a folder and its contents to another folder |
fso.CopyFolder "C:\Files", "C:\Files_Copy" |
| CopyFile |
Copies a file to another location |
fso.CopyFile "C:\Files\file.txt", "C:\Files\file_copy.txt" |
| MoveFolder |
Moves a folder to another location |
fso.MoveFolder "C:\Files", "C:\Files Move" |
| MoveFile |
Moves a file to another location |
fso.MoveFile "C:\Files\file.txt", "C:\Files\file_move.txt" |
| GetDrive |
Returns a Drive object corresponding to the drive in a specified path. Allows getting various information about the drive, such as available space, drive letter, drive type, file system, free space, serial number, share name, total size |
fso.GetDrive("C") AvailableSpace |
| DriveExists |
Checks whether a drive exists |
If fso.DriveExists("D") Then MsgBox "Drive D found" End If |
| GetFolder |
Returns a Folder object corresponding to the folder in a specified path |
fso.GetFolder(app.Path) |
| GetParentFolderName |
Returns a string containing the name of the parent folder of the last component in a specified path | fso.GetParentFolderName(app.Path) |
| GetSpecialFolder |
Returns the special folder specified. One of three options: WindowsFolder (contains files installed by the Windows operating system), SystemFolder (contains fonts, libraries, device drivers), or TemporaryFolder (used to store temp files) |
fso.GetSpecialFolder(TemporaryFolder) |
| GetFile |
Returns a File object corresponding to the file in a specified path | .GetFile "C:\Files\file.txt" |
| FolderExists |
Checks whether a folder exists |
If fso.FolderExists("C:\Files") Then MsgBox "Folder Exists!" End If |
| FileExists |
Checks whether a file exists |
If fso.FileExists("C:\Files\file.txt") Then MsgBox "File Exists!" End if |




