SQL PHP HTML ASP JavaScript articles and free scripts to download
 

Getting the Physical path from virtual path by using Server Mappath object in ASP

In VBScript used in ASP environment, MapPath method returns the physical path of the file or directory from the virtual path. As we setup a site in a remote server we don't have access to the server root path of our site in case of shared hosting. As our site is stored inside a particular folder of the server and by browser or by ftp access we can't move upward to wards the root of the server so it is not possible to get the physical path of any directory or file. Some of the objects like FileSystemObject requires physical path to handle various file and directory related commands. So here we can convert a virtual address of path to physical path by using Server MapPath method.

Let us try some examples where different conditions we will apply to see the results. We kept the file mappath.asp inside my_file directory. So our total physical path looks like this.

C:\Inetpub\wwwroot\my_files\mappath.asp

Note that the object Server.MapPath does not check the existence of actual path, it only maps the given path along with the physical path. So any error in writing directory name or file name will not result in any error message. To make this point clear we are starting with one example of test.asp which is not existing inside the my_file directory. Here is the command and the output
response.write(Server.MapPath("test.asp")

The output of the above line is here

C:\Inetpub\wwwroot\my_files\test.asp

Here we have kept the code inside mappath.asp file and it has displayed the above line. Now let us try with different files and names , the output of the code is given in italic font.

response.write(Server.MapPath("my_files/mappath.asp")
C:\Inetpub\wwwroot\my_files\my_files\mappath.asp

With or without “/” or “\” mark at the staring of the path
If the file or directory name starts with / or \ then the path is taken as full virtual and it starts from server root. Otherwise it is considered from the path of the file being executed. Here are some examples with outputs shown in Italics.

response.write(Server.MapPath("/my_files/mappath.asp")
C:\Inetpub\wwwroot\my_files\mappath.asp

response.write(Server.MapPath("\test.asp")
C:\Inetpub\wwwroot\test.asp

Note the difference the \ mark at the staring of the file name has done. It has taken the root of the server even though the current script is executed inside my_files directory.

To get the root of the present script running we have to use like this.

response.write(Server.MapPath(".")
C:\Inetpub\wwwroot\my_files

To get the server root we have to use

response.write(Server.MapPath("\")
C:\Inetpub\wwwroot

Server.MapPath is used in different applications and it is useful object in ASP platform.

Further readings
Server.MapPath to convert virtual path to physical path of files and directory.
FileSystemObject:Checking file or folder exist by using File system object .
Getting the file name from full path of the current running script in ASP.
Getting the file extension and file name by using FSO
Deleting file by using DeleteFile in FSO
Getting the Last updating and creation date and time of a file.
Listing all files present inside a folder using FileSystemObject
Reading data from a text file using File System Object
Writing data to a text file using File System Object
Read & Writing data to a text file: Developing a page hit counter
Appending data to a text file: Storing emails in a text file
 
Scripts
PHP
JavaScript
All ASP Tutorials
File Management
MapPath
FileSystemObject
append
name
Updating Time
Listing all files
Reading file
Writing file
Read & Write
File type
Delete File
ASP Sections
Date and time
Declaring array
Form in ASP
Server.MapPath
Date Time & MSSQL
Select Query
File System Object
String Functions
Popular Tutorials
Managing two drop downs
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.