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.