The FileSystemObject and associates
The following objects concerned with manipulating and obtaining information on files, folders and drives are exposed by the scripting runtime:
An overview of the FileSystemObject, Drives, Drive, Folders, Folder, Files and File classes
These seven classes really bring object-oriented access to the file system to Visual Basic. Until now, VB has offered only limited intrinsic access to these services in the form of functions such as the Dir function. Advanced access to the file system had to be through API calls, a lot of which are difficult to use from VB, involving VB-hostile types such as the FILETIME user-defined type. For example, a frequent question in the VB newsgroups is "How do I get the path to the system directory ? ". You could use the GetSystemDirectory API, but it is simpler to use the GetSpecialFolder method of the FileSystemObject. Another question might be "How do I get the time a file was last modified ? ". Using the API approach, this would involve calling the GetFileTime API and then somehow convert from the returned FILETIME type to a Date variable. Now, just use the DateLastModified property of the File object.
The seven classes are logically arranged in an object model like this:
This is a fairly deep hierarchy, but accurately models how drives, folders and files are logically arranged in a real file system. Fortunately, it is rarely necessary to do a lot of navigation around the object hierarchy to get things done, because the topmost FileSystemObject class offers a number of shortcut methods to handle drives, folders and files. For example, you could delete a file by navigating to that file, get a reference to that file and then call its Delete method, or you could simply call the DeleteFile method of the FileSystemObject. Both approaches are exemplified below:
The deep approach
Dim FileSys As FileSystemObject Dim File As File Dim Folder As Folder Dim Drive As Drive Set FileSys = New FileSystemObject Set Drive = FileSys.Drives("C") Set Folder = Drive.RootFolder Set Folder = Folder.SubFolders("Temp") Set File = Folder.Files("MyFile.txt") File.Delete
The shallow approach
Dim FileSys As FileSystemObject Set FileSys = New FileSystemObject FileSys.DeleteFile "C:\Temp\MyFile.txt"
The latter approach is not only simpler and more elegant, it is also likely to execute a lot faster. However, there are times when the "deep" approach is preferred, such as when iterating through a large number of folders or files.
Navigating the hierarchy
The FileSystemObject is the externally creatable class. Instances of the six other classes are returned to you via the various methods and properties. From an instance of the FileSystemObject you access a drive via its Drives collection. From a drive you access its folders via its RootFolder property to get a reference to the root folder. Given this reference you can drill your way down the folder tree by recursively accessing the SubFolders collection of a Folder object. Also, from that object you can gain access to a file by retrieving a File object from Folder object's Files collection. To move upwards in the hierarchy, use the ParentFolder or Drive properties of the Folder and File objects to access their parent folder or drive, respectively.
Members of the Drives, Folders and Files classes
These classes are collections of Drive, Folder and File objects, respectively. They are not of the generic VBA.Collection type, but are custom collection classes. All three classes have a Count and an Item property (the default). In addition, the Folders class has an Add method that you can use to create folders. The collection classes support iterating with the For...Each...Next syntax.
To get an idea of the functionality of the non-collection classes it is probably most instructive to see a list of their methods and properties:
Members of the FileSystemObject class
Members of the Drive class
Members of the Folder class
Members of the File class
The FileSystemObject sample displays properties of drives, folders and files on the system.
The TextStream Class
The TextStream class makes it easy to work with text files. There isn't anything in the class that you can't do with the intrinsic VB file functions, but it sure makes it a lot easier. All the lower-level details are taken care of by the class internals, leaving it to the developer to handle the higher-level file I/O in an object-oriented way. In order to work with a TextStream you need a working FileSystemObject object, because you need its two methods CreateTextFile and OpenTextFile. The former creates and opens a physical file and hands you over a TextStream object that you can write to. The OpenTextFile method opens an existing file for reading or writing (you can't do both at the same time). If you have a live File object you can also use its OpenAsTextStream method to same effect.
The class maintains an internal file pointer that indicates the current position in the stream during reading or writing. The Line property returns the current line and the Column property returns the current column, i.e. the character offset from the start of the line. There are three methods for reading that allow you to read one character at a time, one line at a time or the whole file in one go. There are also three methods for writing, one for writing a line, one for writing an arbitrary number of characters and one for writing blank lines.
Members of the TextStream class