古いコードを見直していたところ、次のコードが見つかりました (foo.asp 内):
Const ASP_FILENAME = "foo.asp" ' TODO: Update this to the name of this file (if changed)
この変数は、エラーのログ記録にのみ使用されます。(つまり、「foo.asp のエラー - xxxxx オブジェクトを作成できませんでした。」) これを回避する方法はありますか?
ありがとう!
古いコードを見直していたところ、次のコードが見つかりました (foo.asp 内):
Const ASP_FILENAME = "foo.asp" ' TODO: Update this to the name of this file (if changed)
この変数は、エラーのログ記録にのみ使用されます。(つまり、「foo.asp のエラー - xxxxx オブジェクトを作成できませんでした。」) これを回避する方法はありますか?
ありがとう!
解析Request.ServerVariables("url")
してファイル名部分を取得できます。グーグル検索でこのコードが見つかりました。私はクレジットを主張していませんが、実際にはより理にかなっているように見える SCRIPT_NAME サーバー変数を使用し、配置されている可能性のあるアカウントに URL の書き換えも行っています。
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)
廃止された Aspfaq.com から ( Archive.orgに感謝):
現在の URL / ページの名前を取得するにはどうすればよいですか?
これはとても簡単ですが、2 つの部分があります。
現在のファイルの名前を取得するには、次のいずれかを使用できます。
<%
Response.Write Request.ServerVariables("SCRIPT_NAME") & "<br>"
Response.Write Request.ServerVariables("PATH_INFO") & "<br>"
Response.Write Request.ServerVariables("URL") & "<br>"
%>
そのパスをローカルにする (たとえば、FileSystemObject で使用する) には、結果に server.mappath() メソッドを適用するだけです。
http:// または https:// プレフィックスを含む URL 全体を取得するには、次のようにします。
<%
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
%>
ページ名のみを取得するには、次のようにします。
<%
scr = Request.ServerVariables("SCRIPT_NAME") & "<br>"
if instr(scr,"/")>0 then
scr = right(scr, len(scr) - instrRev(scr,"/"))
end if
response.write scr
%>
または、IF ロジックを使用しない場合:
<%
scr = Request.ServerVariables("SCRIPT_NAME") & "<br>"
loc = instrRev(scr,"/")
scr = mid(scr, loc+1, len(scr) - loc)
response.write scr
%>
今。ファイルが別のファイル内の #INCLUDE である場合、上記のスクリプトは CALLING ファイルの名前を生成します (インクルードされたファイルは最初に呼び出しスクリプトに統合されるため、その中の ASP はすべて「親のコンテキストで実行されます。 ' ファイル)。これを回避する 1 つの方法は、各インクルード ファイルをロードする前に current_filename 変数を再設定することです。次に例を示します。
<%
current_filename = "filetoinclude.asp"
%>
<!--#include file='filetoinclude.asp'-->
(いいえ、current_filename を変数として #INCLUDE ディレクティブに渡そうとしないでください。記事 #2042 を参照してください。)
次に、filetoinclude.asp で:
<%
Response.Write "Current file: " & current_filename
%>
もちろん、各インクルード ファイル内にファイル名を簡単にハードコードすることもできます。しかし、その解決策は、その情報を少なくともある程度動的に取得するという目的を幾分無効にするだろうと思います。
[ここに個別の喉をきれいにするための咳を挿入] の誰もがレガシー アプリケーションの保守とサポートに今でもクラシック ASP を使用しているとは言いませんが、私は最近、同様のことを行う必要がありました。「Classic ASP では無理だ」という回答に甘んじることを拒否して、私は方法を探し始め、次の解決策を思いつきました。
このアプローチは基本的に、基礎となる OS コマンドを活用して、それがファイル インクルード ( ) であるかスクリプト自体 ( ) であるかに関係なく、現在のファイル名を文字通り決定します(その結果は__FILE__
、PHP でマジック定数を使用するのと同じです)。*.inc
*.asp
まず、いくつかのサニタイズをサポートします (必要に応じて、他のより「最適な」方法でこれを行うことができます):
'Regex helpers
Function NewRegex(ByVal pattern, ByVal ignore_case, ByVal global)
Set NewRegex = New RegExp
NewRegex.Pattern = pattern
NewRegex.IgnoreCase = ignore_case
NewRegex.Global = global
End Function
Function RegexMatch(ByVal pattern, ByVal subject)
RegexMatch = RegexMatches(subject, pattern, True, False)
End Function
Function RegexMatches(ByVal subject, ByVal pattern, ByVal ignore_case, ByVal global)
RegexMatches = NewRegex(pattern, ignore_case, global).Test(subject)
End Function
そして今、「反省」の時が来ました:
Function GetCurrentFilename(ByVal uniqueId)
'1. Enforce uniqueId format
If Not RegexMatch("^[0-9a-f]+$", uniqueId) Then
Exit Function
End If
'2. Use findstr to scan "readable" files in current directory for uniqueId
Dim shell, cmd, process, fs, filename
Set shell = Server.CreateObject("WScript.Shell")
'See findstr /? for details on switches used below
'cmd = C:\Windows\system32\cmd.exe /c findstr /P /M /C:"uniqueId" "C:\Inetpub\wwwroot\includes\*"
cmd = shell.ExpandEnvironmentStrings("%COMSPEC%") & " /c findstr /P /M /C:""" & uniqueId & """ """ & Server.MapPath(".") & "\*"""
Set process = shell.Exec(cmd)
'3. Use Scripting.FileSystemObject to return the filename portion of the first result returned
Set fs = Server.CreateObject("Scripting.FileSystemObject")
GetCurrentFilename = fs.GetFileName(process.StdOut.ReadLine())
Set fs = Nothing
Set process = Nothing
Set shell = Nothing
End Function
次に、現在のファイル名を「検査」したいファイル内に、次の行をドロップして、現在のディレクトリ内の他のファイルに存在してはならない一意の識別子を渡します。
'myfile.inc
Response.Write "This is in " & GetCurrentFilename("908ab098c")
結果:
This is in somefile.inc
ああ、これを何に使用する必要があるかに興味がある場合は、単純なブレークポイント関数で使用すると次のようになります。
Function Breakpoint(ByVal line_no, ByVal uniqueId, ByVal msg)
Dim fn
fn = GetCurrentFilename(uniqueId)
Response.Write "[!] Breakpoint hit at Line " & CLng(line_no) & " in " & fn & ": " & Server.HtmlEncode(msg) & vbNewLine
Response.End
End Function
コードの 20 行目に、独自のブレークポイントを追加する場合は、次のようになります。
Breakpoint 20, "B0001", "Some debug output here"
出力:
[!] Breakpoint hit at Line 20 in somefile.inc: Some debug output here
ハッピーコーディング!
server.mappath が従来の ASP に存在するかどうかはわかりませんが、存在する場合は if を使用してページのファイル名を知ることができます。