ASP MapPath to Convert Physical Path to Virtual Path

When you enter an Internet link, you are actually entering a virtual path. The URL tells you nothing about the actual location of the resource on a web server. Certain server objects, like the FileSystemObject, require an actual physical path. The MapPath function accepts a virtual path as a parameter, and returns the physical path of the file or directory.


The statement above returns the physical path to the root of the domain name, usually, but not always as shown below.



The statement above returns the folder of the current file running the script, an example is shown below.



The statement above returns the parent directory of the folder containing the file currently running the script, for example if the script is:an example is shown below.

C:\Inetpub\wwwroot\department\ my_files\my_page.asp

Server.MapPath("..") returns the path shown below.


If the path passed to Server.MapPath starts with either a forward slash (/) or or a backward slash (\), the MapPath() returns a full physical path.


Returns C:\WebApps\department\my_files

If Path doesn't start with a slash, the MapPath() returns a path relative to the directory of the request being processed.

Get virtual path from physical path

Sometimes, you want to convert a physical path to a virtual path, for example to create a link. To do that you could use the code shown below.

Dim mPath, Result

mPath = Server.MapPath(".")
Set Regexp = CreateObject("VBScript.RegExp")
Regexp.Pattern = ".:\\inetpub\\domainName\\"
Result = Regexp.Replace(mPath, "http://domainName/")

This code creates a regular expression object and sets it's search pattern to the websites physical address. It then uses the path returned from Server.MapPath with the replace method of the regular expression object to replace the websites physical address with its virtual address to use in a link.