Active Server Corner
Quick Site Search
What's Inside
Home
Activity
Books
Excerpts
In-Depth
CodeLibrary
Columns
ActiveTalk
COMSutra
Cornered!
My 3 Cents
Square ONE
Downloads
Events
FAQ's
Jobs
Search
Tools
Product Reviews
Tutorials
Site Info
About Us
Advertising Info
Contact Us
Privacy Policy
Terms of Use
Write for Us
  CodeLibrary @ Active Server Corner   The source for source code

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

  ' Obtain the virtual file path
  lsPath = Request.ServerVariables("SCRIPT_NAME")

  ' Split the path along the /s. This creates an
  ' one-dimensional array

  arPath = Split(lsPath, "/")

  ' The last item in the array contains the file name
  GetFileName =arPath(UBound(arPath,1))
End Function
<SCRIPT>

<SCRIPT Language=VBScript RUNAT=SERVER>
Function getFilePath()
  Dim lsPath, arPath

  ' Obtain the virtual file path. The SCRIPT_NAME
  ' item in the ServerVariables collection in the
  ' Request object has the complete virtual file path

  lsPath = Request.ServerVariables("SCRIPT_NAME")
   
  ' Split the path along the /s. This creates an
  ' This creates an one-dimensional array

  arPath = Split(lsPath, "/")

  ' Set the last item in the array to blank string
  ' (The last item actually is the file name)

  arPath(UBound(arPath,1)) = ""

  ' Join the items in the array. This will
  ' give you the virtual path of the file

  GetFilePath = Join(arPath, "/")
End Function
</SCRIPT>

 






Copyright © 1997-2000 Active Server Corner. All rights reserved.

Sponsored by Coastline Web Hosting in Santa Barbara, California