Finding physical paths to files and folders in ASP - Server.MapPath
When working with files and folders on the server it is important to be able to find the physical path. The Server.MapPath
command will do this. It converts a relative path to a physical path.
Finding the path to a file
Server.MapPath is easy to use to find the physical path of a file that is in the same folder as the script. It just takes that file
name as a parameter. In our examples we are using Response.Write to display the paths but they could be used in another function or written
to a variable for later use.
Response.Write Server.MapPath("filename")
filename would be the name and extension of the file, such as "somename.asp"
Finding the current folder/directory
The following code will display the physical path to the folder containing the script that runs the code. It does not include a trailing
backslash character.
Response.Write Server.MapPath(".")
Finding the root path
The following code will display the physical path to the root of the web site.
Response.Write Server.MapPath("/")
Finding the parent directory
Parent paths can be found using two dots (..) to navigate upwards in the directory structure.
Response.Write Server.MapPath("../filename.asp")
It is possible to block the use of parent paths in IIS if a server administrator does not want their users to have this functionality. More
on this - here.
Notes:
The sample code in these tutorials is written for VBScript in classic ASP unless otherwise stated. The code samples can be freely copied.