I'm an amateur VB scripter.
I am creating a script to output the ID. The file contains the line "ad.annnet.id = 564654068". It is necessary to output "ID: 564654068"
With New RegExp
.Pattern = "\nID=(\d )"
Echo .Execute(CreateObject("Scripting.FileSystemObject").OpenTextFile("this.conf").ReadAll)(0).Submatches(0)
End With
CodePudding user response:
There are multiple issues with the script, the actual cause of the "File not found" error is as @craig pointed out in their answer the FileSystemObject
can't locate the file "this.conf". This is because the OpenTextFile()
method doesn't support relative paths and expects an absolute path to the file whether it is in the same directory as the executing script or not.
You can fix this by calling GetAbsolutePathName()
and passing in the filename.
From Official Documentation - GetAbsolutePathName Method
Assuming the current directory is c:\mydocuments\reports, the following table illustrates the behaviour of the
GetAbsolutePathName
method.
pathspec (JScript) pathspec (VBScript) Returned path "c:" "c:" "c:\mydocuments\reports" "c:.." "c:.." "c:\mydocuments" "c:\" "c:" "c:" "c:.\may97" "c:.\may97" "c:\mydocuments\reports*.*\may97" "region1" "region1" "c:\mydocuments\reports\region1" "c:\..\..\mydocuments" "c:....\mydocuments" "c:\mydocuments"
Something like this should work;
'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
Update: It appears you can use path modifiers in OpenTextFile()
after all (thank you @LesFerch), so this should also work;
'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
Another issue is the current RegExp
pattern will not match what you are expecting, would recommend using something like Regular Expressions 101 to test your regular expressions first.
CodePudding user response:
You will never get the value/display that you're looking for if the output is
File Not Found
The regexp is meaningless until the path to the file is specified. As it stands, the "this.conf" file must be in the same location as the script itself - and from the error, I'm assuming that's not the case.