This one is pretty easy, but there are two parts. To retrieve the name of the current file, you can use any of these:
<% Response.Write Request.ServerVariables("SCRIPT_NAME") & "<br>" Response.Write Request.ServerVariables("PATH_INFO") & "<br>" Response.Write Request.ServerVariables("URL") & "<br>" %> |
E.g., http://localhost , you will get the result: “/Default.asp”
To make that path local (for example, to use with FileSystemObject), just apply the server.mappath() method to the result. To get the entire URL, including the http:// or https:// prefix, you can do this:
<% prot = "http" https = lcase(request.ServerVariables("HTTPS")) if https <> "off" then prot = "https" domainname = Request.ServerVariables("SERVER_NAME") filename = Request.ServerVariables("SCRIPT_NAME") querystring = Request.ServerVariables("QUERY_STRING") response.write prot & "://" & domainname & filename & "?" & querystring %> |
To get the page name ONLY, use something like this:
<% scr = Request.ServerVariables("SCRIPT_NAME") & "<br>" if instr(scr,"/")>0 then scr = right(scr, len(scr) - instrRev(scr,"/")) end if response.write scr %> |
Or, without the IF logic:
<% scr = Request.ServerVariables("SCRIPT_NAME") & "<br>" loc = instrRev(scr,"/") scr = mid(scr, loc+1, len(scr) - loc) response.write scr %> |
Now. If your file is an #i nclude within another file, the above scripts will produce the name of the CALLING file (since the included file is first integrated into the calling script, then the ASP within it is all executed in the context of the 'parent' file). One way you can work around this is to re-populate a current_filename variable before loading each include file, for example:
<% current_filename = "filetoinclude.asp" %> <!--#i nclude file='filetoinclude.asp'--> |
(And no, don't try passing current_filename as a variable to the #i nclude directive; see Article #2042.) Then, in filetoinclude.asp:
<% Response.Write "Current file: " & current_filename %> |
Of course, you could just as easily hard-code the filename inside of each include file. But I suppose that solution would somewhat defeat the purpose of retrieving that information at least somewhat dynamically.
|