Error using VBScript in BGInfo File not found error

问题描述 投票:0回答:4

我是业余 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
regex vbscript
4个回答
1
投票

脚本存在多个问题,“找不到文件”错误的实际原因是@craig他们的回答中指出

FileSystemObject
找不到文件“this.conf”。这是因为
OpenTextFile()
方法不支持相对路径并且需要文件的绝对路径,无论它是否与执行脚本位于同一目录中。

您可以通过调用

GetAbsolutePathName()
并传入文件名来解决此问题。

来自官方文档-GetAbsolutePathName方法


假设当前目录是c:\mydocuments eports,下表说明了

GetAbsolutePathName
方法的行为。

路径规范(JScript) 路径规范(VBScript) 返回路径
“c:” “c:” "c:\我的文档 电竞”
“c:..” “c:..” "c:\我的文档"
“c:\” “c:” “c:”
"c:.\may97" "c:.\may97" "c:\我的文档 电子端口*.*\may97"
“区域 1” “区域 1” "c:\我的文档 电子竞技 区域 1"
"c:\..\..\mydocuments" "c:....\mydocuments" "c:\我的文档"

像这样的东西应该有用;

'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

更新:看来毕竟你可以在

OpenTextFile()
中使用路径修饰符(谢谢@LesFerch),所以这也应该有效;

'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" '.\ denotes the current directory
Dim filecontent: filecontent = fso.OpenTextFile(filename).ReadAll

另一个问题是当前的

RegExp
模式将不符合您的期望,建议您先使用 Regular Expressions 101 之类的东西来测试您的正则表达式。


0
投票

在bginfo中,点击CustomNew,输入ID作为标识符,勾选VB Script file单选按钮,然后点击Browse选择一个您用下面的代码保存的VBScript文件。然后在bginfo显示区添加“ID”项。注意:“找不到文件”错误通过使用 AppData 环境变量引用 AnyDesk 数据文件来解决。

Const ForReading = 1
Set oWSH = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
AppData = oWSH.ExpandEnvironmentStrings("%APPDATA%")
DataFile = AppData & "\AnyDesk\system.conf"
Set oFile = oFSO.OpenTextFile(DataFile,ForReading)
Do Until oFile.AtEndOfStream
  Line = oFile.ReadLine
  If InStr(Line,"ad.anynet.id") Then ID = Split(Line,"=")(1)
Loop
oFile.Close
Echo ID

请注意,数据通过一个或多个Echo语句返回到bginfo。如果在 bginfo 之外测试此脚本,请将 Echo 更改为 WScript.Echo.


0
投票

如果输出是

,你永远不会得到你正在寻找的值/显示

找不到文件

在指定文件路径之前,正则表达式没有意义。就目前而言,“this.conf”文件必须与脚本本身位于同一位置 - 从错误来看,我假设情况并非如此。


-2
投票

你想过这个吗?目前正试图在我的工作中完成同样的事情

© www.soinside.com 2019 - 2024. All rights reserved.