views:

2106

answers:

2

How do you get the name of the current virtual directory using ASP Classic? In ASP.NET you can use Request.ApplicationPath() to find this.

For example, let's say you have a URL like this:

http://localhost/virtual_directory/subdirectory/file.asp

In ASP.NET, Request.ApplicationPath() would return /virtual_directory

+1  A: 

Try using: Request.ServerVariables("SCRIPT_NAME")

or try using Request.ServerVariables("INSTANCE_META_PATH") if that does not work for you.

For a list of other server variables try this link:

http://www.w3schools.com/asp/coll_servervariables.asp

Jobo
+2  A: 

You can get the virtual path to the file from one of several server variables - try either:

  • Request.ServerVariables("PATH_INFO")
  • Request.ServerVariables("SCRIPT_NAME")

(but not INSTANCE_META_PATH as previously suggested - this gives you the meta base path, not the virtual path you're expecting).

Either server variable will give you the virtual path including any sub-directories and the file name - given your example, you'll get "/virtual_directory/subdirectory/file.asp". If you just want the virtual directory, you'll need to strip off everything after the second forward slash using whatever method you prefer for plucking a directory out of a path, such as:

s = Request.ServerVariables("SCRIPT_NAME")
i = InStr(2, s, "/")
If i > 0 Then
    s = Left(s, i - 1)
End If

or:

s = "/" & Split(Request.ServerVariables("SCRIPT_NAME"), "/")(1)
Simon Forrest