我是一名業余 VB 腳本撰寫者。
我正在創建一個腳本來輸出 ID。該檔案包含“ad.annnet.id = 564654068”這一行。需要輸出“ID:564654068”
With New RegExp
.Pattern = "\nID=(\d )"
Echo .Execute(CreateObject("Scripting.FileSystemObject").OpenTextFile("this.conf").ReadAll)(0).Submatches(0)
End With
uj5u.com熱心網友回復:
有與腳本多個問題,在實際的原因“找不到檔案”的錯誤是@craig中指出,他們的回答中FileSystemObject無法找到檔案“this.conf”。這是因為該OpenTextFile()方法不支持相對路徑,并且無論檔案與執行腳本是否在同一目錄中,都需要檔案的絕對路徑。
您可以通過呼叫GetAbsolutePathName()并傳入檔案名來解決此問題。
來自官方檔案 - GetAbsolutePathName 方法
假設當前目錄是 c:\mydocuments\reports,下表說明了該
GetAbsolutePathName方法的行為。
路徑規范(JScript) 路徑規范(VBScript) 回傳路徑 “C:” “C:” "c:\mydocuments\reports" “C:..” “C:..” "c:\mydocuments" “C:\” “C:” “C:” "c: . \may97" "c: . \may97" "c:\mydocuments\reports*.*\may97" “區域1” “區域1” "c:\mydocuments\reports\region1" "c:\..\..\mydocuments" "c:....\mydocuments" "c:\mydocuments"
像這樣的事情應該有效;
'Read the file from the current directory (can be different from the directory executing the script, check the execution).
Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
Dim filename: filename = "this.conf"
Dim filepath: filepath = fso.GetAbsolutePathName(filename)
Dim filecontent: filecontent = fso.OpenTextFile(filepath).ReadAll
另一個問題是當前RegExp模式與您期望的不匹配,建議先使用正則運算式 101 之類的東西來測驗您的正則運算式。
uj5u.com熱心網友回復:
如果輸出是
檔案未找到
在指定檔案路徑之前,正則運算式是沒有意義的。就目前而言,“this.conf”檔案必須與腳本本身位于同一位置 - 從錯誤來看,我假設情況并非如此。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/358688.html
