Retrieving file names and file paths in ASP scripts
by Manohar Kamath
August 19, 1999
It's not that difficult but I thought I'd write a reusable script
anyway. To get file information, one has to ultimately rely on the ServerVariables
collection in the Request object. Assume a complete file path such as
/myFolder/mySubFolder/myASPFile.asp
The ServerVariables collection does not have any variable that
contains just the file name, myASPFile.asp in this case. So people resort to string
parsing - search the string until the last / is found, then get "cut" the string
into two. The first part would be the file folder path and the second part would be the
file name.
The above way is perfectly fine, except it is long winded. Here I
present two scripts - one to find the file name and another to get the file folder. The
first method - getFileName(), in the above example, would produce a result like
myASPFile.asp
and the second method - getFilePath() would produce
/myFolder/mySubFolder/
Just add these scripts to any ASP page and call the functions
accordingly. Since the functions are coded within the <SCRIPT..>..</SCRIPT>
tags, it does not matter what server scripting language you use.
Tip: To get the physical
folder of the file, just do a Server.MapPath(getFilePath())
<SCRIPT Language=VBScript RUNAT=SERVER>
Function getFileName()
Dim lsPath, arPath
lsPath = Request.ServerVariables("SCRIPT_NAME")
arPath = Split(lsPath, "/")
GetFileName =arPath(UBound(arPath,1))
End Function
<SCRIPT>
<SCRIPT Language=VBScript RUNAT=SERVER>
Function getFilePath()
Dim lsPath, arPath
lsPath = Request.ServerVariables("SCRIPT_NAME")
arPath = Split(lsPath, "/")
arPath(UBound(arPath,1)) = ""
GetFilePath = Join(arPath, "/")
End Function
</SCRIPT>
|