When ASP is installed, the FileSystemObject and the TextStream object are automatically installed along with it. The ASP FileSystemObject allows you to work with drives, folders, and files on the server. The TextStream object allows you to create, read, and write files.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site

Working with Files in ASP

When ASP is installed, the FileSystemObject and the TextStream object are automatically installed along with it. The ASP FileSystemObject allows you to work with drives, folders, and files on the server. The TextStream object allows you to create, read, and write files.

In certain instances, you may not be sure that a specific file exists. In that case, you should use the FileSystemObject's FileExists method before attempting to access the file. Example code for this is shown below.

<%
Dim objFS

Set objFS = 
  Server.CreateObject("Scripting.FileSystemObject")
If objFS.FileExists("c:\asp\newfolder\newfile.txt") Then
  Response.Write "File exists"
Else
  Response.Write "File does not exist"
End If 

Set objFS = Nothing
%> 

When you enter a URL into the address bar in your Web browser, it consists of a domain name followed by a file path. The URL file path has no relation to the actual file path on a physical storage device because a website may be located or moved from one physical storage location to another without changing the URL of any file on that Web site. The path you enter into a Web browser is a "virtual path".

The ASP Server object represents the Web server itself and has a method named MapPath that accepts a virtual path and returns the actual physical path. Below is how a call to the MapPath method looks.

Server.MapPath(path)

For example, you could use the code shown below to determine the actual physical location of the web server.

<%
Dim strPath

strPath = Server.MapPath("/")
response.write(strPath)
%>

The resulting response from the above code might be "c:\inetpub\wwwroot". Note that you don't need to create the Server object because if the Web server is running, the server object already exists.

Generally, you would use the MapPath method to determine the actual physical path of a file from its virtual path, as shown below.

<%
Dim strPath

strPath = Server.MapPath("/foldername/filename.asp")
response.write(strPath)
%>

If the path argument doesn't start with a slash (/ or \), MapPath assumes that it is a path relative to the .asp file being processed. For example, if an .asp file located at the physical path c:\inetpub\wwwroot\myfolder\myfile1.asp calls the MapPath method with the code shown below.

<%
Dim strPath

strPath = Server.MapPath("../foldername/filename.asp")
response.write(strPath)
%>

The resulting response would be "c:\inetpub\wwwroot\foldername\filename.asp".

Programming Sections
ASP

Get paid cash typically $4 to $25 for each survey you complete!

Self Improvement Newsletters

[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2007 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269