EmersonH.Com

Transforming Knowledge into Power

  • Increase font size
  • Default font size
  • Decrease font size
Home VB 6 File System Object OBJ FileSystemObject component

FileSystemObject component

E-mail Print

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

Object/collection Type Description
FileSystemObject Main object Allows creating, deleting, manipulating, and getting information about drives, folders, files
Drive Object Allows getting information about a drive
Drives Collection Lists drives available on the system (both physical and logical)
File Object Allows creating, deleting, and moving files, as well as getting file properties
Files Collection Lists all files in a given folder
Folder Object Allows creating, moving, and deleting folders, as well as getting information about a particular folder
Folders Collection Provides a list of all folders in a Folder
TextStream Object Allows reading and writing text files

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
Last Updated on Monday, 21 December 2009 16:59