Search code examples
asp-classicvirtual-directory

How do you get the name of the current virtual directory using ASP Classic?


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


Solution

  • 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)