The scrrun.dll defines several objects for managing files. These facilities include the FileSystemObject and TextStream classes. FileSystemObject represents your PCs file system and includes such capabilities as creating temporary files, managing directories, and managing files. The TextStream class is defined to represent files as a contiguous stream of text data and makes managing text file content much easier than using the legacy VB Open, Close, Line Input, and other such commands. In this weeks article I will demonstrate several of the basic capabilities of the Microsoft Scripting Runtime.
Before we begin, as a reminder you will need to add a reference to the Scripting Runtime library in VB6 from the Project|References menu. (Check the Microsoft Scripting Runtime reference and click OK in the Add References dialog.) The examples will use early bound objects rather than late bound objects using CreateObject, but you can use CreateObject and Variant types to dynamically load external libraries at runtime.
Creating a Temporary File
Sometimes you need to create a temporary file. Perhaps you need to persist the state of an object to the users PC or make a backup file during a save operation. This example demonstrates how to use the FileSystemObject in the Scripting Runtime library to get a unique temporary file name and copy the contents to the temporary file.
The following Backup function takes a file name and creates a copy of the file using FileSystemObject.CopyFile and FileSystemObject.GetTempName.
Private Sub Backup(ByVal FileName As String) Dim FS As New FileSystemObject Call FS.CopyFile(FileName, FS.GetTempName)End Sub
A production application may want to perform some additional steps. You may want to call the FS.FileExists method to ensure that the source file represented by FileName exists, and you may want to copy the backup to a specific location. GetTempName does not create a file nor does it include path information. By default the copy will be created in the VB98 folder where you installed VB unless you specify a path, in addition to the temp file name.
Reading and Writing Text Files
The FileSystemObject, in conjunction with the TextStream class, can be used to manage text files. FileSystemObject.OpenTextFile takes four parameters and returns an instance of a TextStream object, which in turn has some of its own capabilities. Only the FileName is a required parameter of OpenTextFile. FileName represents the file to manage. The second argument, IOMode, is one of three defined constants: ForAppending, ForReading, and ForWriting. The third parameter, Create, is a Boolean. The fourth parameter is one of TriStateFalse, TriStateMixed, TriStateTrue, or TriStateUseDefault.
Each of the four parameters describes how operations behave on the open file. The last three parameters are Optional. If you do not provide an argument for the optional parameters then the default IOMode is ForReading, Create is False, and Format is TristateFalse. The first three arguments are self-explanatory. The fourth argument, Format, indicates whether the contents of the text file are treated as Unicode (16-bit) characters or ASCII (8-bit) characters.
The two examples that follow demonstrate creating a FileSystemObject, opening a text file, and reading or writing, respectively, to the file using the TextStream object.
1: ' Open a file and add some text using a TextStream object2: Private Function ReadTextFile(ByVal FileName As String) As String3: 4: Dim FS As New FileSystemObject5: Dim Stream As TextStream6: 7: Call Backup(FileName)8: Set Stream = FS.OpenTextFile( _9: FileName, ForReading, False)10: 11: ReadTextFile = Stream.ReadAll()12: Stream.Close13:14: Set FS = Nothing15:16: End Function
The example demonstrates a function that takes a FileName argument and returns all of the contents of a text file. Note the use of the TextStream object on lines 8, 11, and 12. The FileSystemObject is responsible for returning an opened stream object and the TextStream objectnamed Stream in the listingactually closes the stream. Writing text to a file requires simply that we pass the name of the file to write to and the text to write, to a subroutine and open the file for writing.
1: Private Sub WriteTextFile(ByVal FileName As String, ByVal Text As String)2: 3: Dim FS As New FileSystemObject4: Dim Stream As TextStream5: 6: Set Stream = FS.OpenTextFile( _7: FileName, ForWriting, False)8: 9: Call Stream.Write(Text)10: Stream.Close11: Set FS = Nothing12: End Sub
Notice that the code is the WriteTextFile subroutine is only slightly different than the ReadTextFile function. The operations for reading and writing differ only slightly semantically, so then should the implementations necessarily differ only slightly. A final note: a robust implementation might use an On Error Goto label statement to catch errors that may occur in the read or write procedures.
The Microsoft Scripting Runtime offers several classes rich with file system and file content management capabilities. Spend a little time exploring the help documentation before you write file management utilities from scratch. There is a good chance that what you need already exists in the scrrun.dll.
About the Author
Paul Kimmel is a freelance writer for Developer.com and CodeGuru.com. He is the founder of Software Conceptions, Inc, found in 1990. Paul Kimmel is available to help you build Visual Basic.NET solutions. You may contact him at firstname.lastname@example.org.