IT:FileSystemObject

From Stiki

Jump to: navigation, search

In Visual Basic, the FileSystemObject object allows interaction with the underlying file system of the operating system, including the various drives, folders and files stored therein.

This page briefly describes the usage, properties, and methods of the FileSystemObject object and its associated objects. More details on all of these can be found in the relevant VB help files. However there is a lack of a useful summary of these properties and methods, and some of the help files have poor links; hence this page. I have also included some details that are not in the help files.


Contents

FileSystemObject objects

These objects refer to the totality of the file system for the OS. Any of the other object types listed on this page must be accessed through a FileSystemObject object.

Available properties and methods for a FileSystemObject object:
Property Brief Description
Drives Returns a Drives collection containing a Drive object for each drive that the calling program can see.
Method Brief Description
BuildPath Returns a file path created by concatenating two specified paths (and inserting an additional "\" if required). The paths do not need to exist.
CopyFile Copies the file specified in the path to another location (with an option to overwrite files in the destination location). Wildcards can be used in the source filename. Returns error 76 ("Path not found") is the file does not exist.
CopyFolder Copies the folder specified in the path to another location (with an option to overwrite files and folders in the destination location). Wildcards can be used in the source folder name. Returns error 76 ("Path not found") is the folder does not exist.
CreateFolder Creates a folder with the specified path. Returns error 58 ("File already exists") if the path already exists. Returns error 76 ("Path not found") is the parent directory of the specified path does not already exist.
CreateTextFile Creates a text file with a specified filename in the folder, and opens it as a TextStream object (with an option to overwrite a file in the destination location). The text file can be created as an ASCII file or a Unicode file.
DeleteFile Deletes the file specified in the path (with an option to prevent deletion of read-only files and folders). Wildcards can be used in the target folder name. Returns error 53 ("File not found") if the file does not exist. Nothing happens if the path refers to a folder.
DeleteFolder Deletes the folder specified in the path (with an option to prevent deletion of read-only files and folders). Wildcards can be used in the target folder name. Returns error 76 ("Path not found") if the folder does not exist. Nothing happens if the path refers to a file.
DriveExists Returns True if the specified path refers to a drive (by root directory or network share) that exists, and False otherwise.
FileExists Returns True if the specified path refers to a file (not folder) that exists, and False otherwise.
FolderExists Returns True if the specified path refers to a folder (not file, but including root directory) that exists, and False otherwise.
GetAbsolutePathName Returns a complete and unambiguous path for the path specified (note: no trailing "\" for folders, except for root directories). This will convert relative paths into absolute paths.
GetBaseName Returns the part of a filename before the extension for a file or folder at the specified path (the full folder name for a folder unless it contains the "." character). The path does not need to exist.
GetDrive Returns a Drive object for the drive at the specified drive letter, root directory path, or network share. Returns error 68 ("Device unavailable") is the specified drive does not exist or cannot be accessed. Returns error 5 ("Invalid procedure call or argument") if a file or folder path is used as the argument.
GetDriveName Returns the root directory of the file or folder at the specified path. The path does not need to exist.
GetExtensionName Returns the extension of the file or folder at the specified path (zero-length string for a folder unless it contains the "." character). The path does not need to exist.
GetFile Returns a File object for the file at the specified path. Returns error 53 ("File not found") if the file does not exist.
GetFileName Returns the full filename of the file or folder at the specified path. The path does not need to exist.
GetFolder Returns a Folder object for the folder at the specified path. Returns error 76 ("Path not found") if the file does not exist.
GetParentFolderName Returns the full folder path of the parent folder of the specified path(note: no trailing "\"). Returns a blank string if the folder is a root directory.
GetSpecialFolder Returns a Folder object for specified folder:
Constant Value Description
WindowsFolder 0 The folder containing the files installed by the Windows operating system (often "C:\WINDOWS").
SystemFolder 1 The folder contains libraries, fonts, and device drivers (often "C:\WINDOWS\system32").
TemporaryFolder 2 The folder used to store temporary files (often "C:\DOCUME~1\Username\LOCALS~1\Temp").

Note that the special folder's path will be in 8.3 format, as can be seen in the example TemporaryFolder above.

GetTempName Returns a randomly generated file name that can be used for created a temporary file.
MoveFile Moves one or more files to another location. Wildcards can be used in the source filename.
MoveFolder Moves one or more folders to another location. Wildcards can be used in the source folder name.
OpenTextFile Opens a text file with a specified path and filename (optionally creating the file if it doesn't already exist) as a TextStream object, returning a reference to the object. The text file can be opened as an ASCII file or a Unicode file.

Drive objects

Available properties for a Drive object:
Property Brief Description
AvailableSpace Returns the size (in bytes) of the total unused space on the drive.
DriveLetter Returns the drive letter (or a zero-length string if the drive has no associated letter).
DriveType Returns the drive type:
Value Description
0 Unknown
1 Removable disk
2 Fixed disk
3 Network drive
4 CD-ROM drive
5 RAM disk
FileSystem Returns the type of file system used on the drive (FAT, NTFS, CDFS).
FreeSpace Returns the size (in bytes) of the unused space available to the user on the drive.
IsReady Returns True if the drive is ready (for example a CD drive contains a valid disc and is ready for access), and False otherwise.
Path Returns the folder path of the drive (note: no trailing "\").
RootFolder Returns a Folder object for the root directory of the drive.
SerialNumber Returns the serial number of the drive as a decimal number.
ShareName Returns the netowrk share name of the drive.
TotalSize Returns the total size (in bytes) of the drive (includes all used and unused space).
VolumeName Sets or returns the volume name.

Folder objects

Available properties and methods for a Folder object:
Property Brief Description
Attributes Sets or returns the attributes of the folder:
Constant Value Description
Normal 0 Normal file. No attributes are set.
ReadOnly 1 Read-only file. Attribute is read/write.
Hidden 2 Hidden file. Attribute is read/write.
System 4 System file. Attribute is read/write.
Volume 8 Disk drive volume label. Attribute is read-only.
Directory 16 Folder or directory. Attribute is read-only.
Archive 32 File has changed since last backup. Attribute is read/write.
Alias 64 Link or shortcut. Attribute is read-only.
Compressed 128 Compressed file. Attribute is read-only.
DateCreated Returns the date and time that the folder was created.
DateLastAccessed Returns the date and time that the folder was last accessed.
DateLastModified Returns the date and time that the folder was last modified.
Drive Returns the root directory of the folder (note: no trailing "\").
Files Returns a Files collection containing a File object for each file within the folder (including hidden and system folders) but not within subfolders.
IsRootFolder Returns True if the folder is a root directory, and False otherwise.
Name Sets or returns the folder name (note: no trailing "\").
ParentFolder Returns the full folder path of the parent folder (note: no trailing "\"). Returns error 91 ("Object variable or With block variable not set") if the folder is a root directory.
Path Returns the full folder path (note: no trailing "\").
ShortName Returns the folder name using the 8.3 naming convention (note: no trailing "\").
ShortPath Returns the full folder path using the 8.3 naming convention (note: no trailing "\").
Size Returns the size (in bytes) of all files and subfolders in the folder.
SubFolders Returns a Folders collection containing a Folder object for each subfolder within the folder (including hidden and system folders).
Type Returns the folder type as a text string (usually "File Folder" for a normal directory). This can be used to identify various unique folders.
Method Brief Description
Copy Copies the folder to another location (with an option to overwrite files and folders in the destination location).
CreateTextFile Creates a text file with a specified filename in the folder, and opens it as a TextStream object (with an option to overwrite a file in the destination location). The text file can be created as an ASCII file or a Unicode file.
Delete Deletes the folder (with an option to prevent deletion of read-only files and folders).
Move Moves the folder to another location.

File objects

Available properties and methods for a File object:
Property Brief Description
Attributes Sets or returns the attributes of the file:
Constant Value Description
Normal 0 Normal file. No attributes are set.
ReadOnly 1 Read-only file. Attribute is read/write.
Hidden 2 Hidden file. Attribute is read/write.
System 4 System file. Attribute is read/write.
Volume 8 Disk drive volume label. Attribute is read-only.
Directory 16 Folder or directory. Attribute is read-only.
Archive 32 File has changed since last backup. Attribute is read/write.
Alias 64 Link or shortcut. Attribute is read-only.
Compressed 128 Compressed file. Attribute is read-only.
DateCreated Returns the date and time that the file was created.
DateLastAccessed Returns the date and time that the file was last accessed.
DateLastModified Returns the date and time that the file was last modified.
Drive Returns the root directory of the file (note: no trailing "\").
Name Sets or returns the filename.
ParentFolder Returns the full folder path of the parent folder (note: no trailing "\").
Path Returns the full file path.
ShortName Returns the filename using the 8.3 naming convention.
ShortPath Returns the full file path using the 8.3 naming convention.
Size Returns the size (in bytes) of the file.
Type Returns the file type as a text string. This is determined by the file extension.
Method Brief Description
Copy Copies the file to another location (with an option to overwrite a file in the destination location).
Delete Deletes the file (with an option to prevent deletion of read-only files).
Move Moves the file to another location.
OpenAsTextStream Opens the file as a TextStream object, returning a reference to the object. The file can be opened as an ASCII file or a Unicode file.

TextStream objects

Essentially allows sequential file access to a text file.

Available properties and methods for a TextStream object:
Property Brief Description
AtEndOfLine Returns True if the file pointer is at the end of a line (immediately preceding an end-of-line marker), and False otherwise.
AtEndOfStream Returns True if the file pointer is at the end of the file, and False otherwise.
Column Returns the column number for the current position of the file pointer. Note that the first character of a line is in column 1.
Line Returns the line number for the current position of the file pointer. Note that the first line is line 1.
Method Brief Description
Close Closes an open file.
Read Returns a string consisting of the next specified number of characters.
ReadAll Returns a string containing the entire contents of the file.
ReadLine Returns a string containing the contents of the file from the current file pointer position to the end of the current line.
Skip Moves the file pointer on a specified number of characters without reading those characters to an output string.
SkipLine Moves the file pointer on to the start of the next line without reading the current line to an output string.
Write Writes a specified string to the file.
WriteBlankLines Writes a specified number of blank lines to the file.
WriteLine Writes a specified string to the file as a complete line (a newline character is appended after writing the string).
Personal tools