When the designers of VBScript were initially putting the language together a few years ago, they wanted a language that was light, so they removed some functionality. Because they didn't want a language that could affect the system on which it was running, they removed the ability to write files to a drive on the system. Somewhere along the way, it was decided that this feature actually is a desirable one. Rather than re-adding that functionality to the native language, they built a new component called the FileSystemObject. The FileSystemObject lets you manipulate the file system on the machine on which it is running. In particular, the FileSystemObject can write folders and files to a drive of the system. In this month's 10-minute solution, I'll show how you can use the FileSystemObject to write text files to a Web server's drive. You can use this component anytime you need to save information to the server.
There are many situations where the ability to write folders and files to a drive of a Web server comes in handy. For example, using the FileSystemObject, you can create comma-delimited files to store transient data. The data file can be stored on the Web server and later moved to a database. The file could be a log file that stores statistics about the Web server, users, or any other data. For instance, one way to build a user counter is to create an ASP application variable that is global to all users. Each time a new user browses the site, you add one to the variable. Whenever the application shuts down, you can write the value of the variable to the text file through the FileSystemObject. When the application restarts, the file can be read and reloaded into the variable. The user counter can maintain its count over an extended period of time. Another way the FileSystemObject can be used is to create folders. User-specific information can be stored in folders. Using a Web application, users can enter their own information, save their own files, and it can all be automated. Everything from creating the folder to cleaning up the folder information (deleting files) can be done programmatically through the FileSystemObject.
Using the FileSystemObject is surprisingly easy. First, you create an instance of the object using the CreateObject method.
Set fs = CreateObject("Scripting.FileSystemObject")
Next, you use the reference returned from the newly created object to call the CreateTextFile method of the FileSystemObject. The first parameter passes the name of the file to create and the second parameter tells the object to overwrite the file if it already exists.
Set a = fs.CreateTextFile(PathAndFileName, True)
You can write out the text of the file using the reference to the file that was returned from the CreateTextFile method using either the Write() method or the WriteLine() method. Write() will write many lines simultaneously while WriteLine() writes one line at a time.
a.Write(WriteThis)
Finally, close the file when you have finished writing to it.
a.Close
Reading a text file back in is just as easy. In this case, you will open an existing file rather than creating a new one using the OpentextFile method. This parameter is used to specify the path and filename of the file to open.
Set fs = CreateObject("Scripting.FileSystemObject")
Set ts = fs.OpenTextFile(PathAndFileName)
The OpenTextFile method will return a reference to the open file. You can loop through the file to read it. You can use the AtEndOfStream property of the file to determine when to stop looping through the file. When you find a line that you would like to use, the ReadLine method will pass the text of the current line to a variable or out to the HTML stream.
Do While Not ts.AtEndOfStream
Response.Write ts.ReadLine & "<br>"
Loop
When you are finished, you should close the file.
ts.Close
Anytime you want to create a file or a folder, or manipulate the file system in another way, the FileSystemObject is ready to help. You can use the FileSystemObject to write a log file that stores statistics about the Web server or even use it to help build a user counter. Click here for a complete example of how to use the FileSystemObject to write a text file to a drive on a Web server. You can copy this page and modify it for your own use.
Charles Caison is an instructor with DBBasics Corporation (http://www.dbbasics.com). He has written for Visual Basic Programmer's Journal, Windows NT Magazine, Microsoft Interactive Developer, and other industry publications. He is the co-author of Professional ADO and RDS with ASP from Wrox Press. He also speaks at Microsoft Developer Days and other industry events. You can reach him at charlesc@dbbasics.com.