ASPFAQs.com
Your source for ASP-related frequently asked questions and answers!


4GuysFromRolla.com : ASP FAQS : FileSystemObject

Question: What, exactly, is the FileSystemObject?

Answer: The FileSystemObject, or FSO, is an often used component to access the web server's file system. For example, you can create files, read the contents of files, determine whether or not a folder or file exists, iterate through the contents of a folder or directory, or any other number of file system-related tasks. You can create an instance of the FileSystemObject component in an ASP page with the following code:

Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

Once you create an instance of this component, you can access its many properties and methods. For example, say that you wanted to output the contents of the text file C:\SomeFile.txt. The following code could be used:

Const fsoForReading = 1

Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

'Open the text file
Dim objTextStream
Set objTextStream = objFSO.OpenTextFile("C:\SomeFile.txt", fsoForReading)

'Display the contents of the text file
Response.Write objTextStream.ReadAll

'Close the file and clean up
objTextStream.Close
Set objTextStream = Nothing
Set objFSO = Nothing

Understand that the FileSystemObject's purpose is to allow an ASP page (or VB program, or WSH file) to have access to the machine's file system. Since your ASP pages reside and are executed on the web server, when using FSO to access the file system, you are accessing the web server's file system. You cannot access the client's file system using the FileSystemObject in your ASP code.

One important thing to keep in mind when using the FileSystemObject is permissions! In Windows NT, there are three types of permissions: Read, Write, and Full Access. Whenever an ASP page attempts to execute some file system command using FSO, the anonymous web user ID is used: IUSR_machinename (i.e., if your webserver is named Bob, the user ID is IUSR_Bob). If IUSR_machinename doesn't have permissions to Read for a particular directory, you cannot use FSO to read the contents of a file in that directory through an ASP page. If IUSR_machinename doesn't have permissions to Write for a particular directory, you cannot use FSO to write a file to that directory through an ASP page. If IUSR_machinename doesn't have Full Access permissions for a particular directory, you cannot use FSO to delete or move a file from that directory through an ASP page.


FAQ posted by Scott Mitchell at 9/25/2000 2:24:49 AM to the FileSystemObject category. This FAQ has been viewed 80,731 times.


Copyright 2017 QuinStreet Inc. All Rights Reserved.
Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers