views:

922

answers:

3

I was just reviewing some old code and found the following (inside foo.asp):

Const ASP_FILENAME = "foo.asp"  ' TODO: Update this to the name of this file (if changed)

The variable is only used for logging errors. (ie. "Error in foo.asp - Could not create xxxxx object.") Is there any way to avoid this?

Thanks!

+2  A: 

You could parse Request.ServerVariables("url") to get the filename portion. A google search found this code, to which i don't claim credit, which uses the SCRIPT_NAME server variable which seems to make more sense indeed, also taking any url rewriting in to account that might be in place:

function getFileName(fpath, returnExtension)
        tmp = fpath
        if instrRev(tmp,"/") > 0 then
              tmp = mid(tmp, instrRev(tmp,"/")+1)
        end if
        if returnExtension = false then
              if instrRev(tmp,".") > 0 then
                    tmp = left(tmp, instrRev(tmp,".")-1)
              end if
        end if
        getFileName = tmp
  end function

  filename = request.ServerVariables("SCRIPT_NAME")
  Const ASP_FILENAME = getFileName(filename, true)
Martijn Laarman
A: 

I dont know if server.mappath exists on traditional asp, but if so you could use if to know the page filename.

Sergio
+3  A: 

These guys explain your options well, so I won't bother.

CJM
Current Url <> Current file name. It could be an include file, for example.
Joel Coehoorn
If that is an issue for the OP, this site suggests a means of handling that scenario too.
CJM