0

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
user692942
  • 16,398
  • 7
  • 76
  • 175
sogohimo
  • 3
  • 1
  • 1
    If the file contains `ad.annnet.id = 564654068` the pattern you have set in the `RegExp` is wrong. Also, the question title seems to have no relevance to the question? – user692942 Nov 16 '21 at 08:12
  • You can't pass a relative path in `OpenTextFile()` it has to be absolute which you can get by passing it into the [`GetAbsolutePathName()` method](https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/scripting-articles/zx1xa64f(v=vs.84)), see [VBscript relative path](https://stackoverflow.com/a/15621773). – user692942 Nov 17 '21 at 09:57
  • @user692942 , could you help correct the my code and show how it might look from your point of view, please? I would be very grateful to you – sogohimo Nov 17 '21 at 10:47
  • The link, provided in my previous comment should help. You can [edit] the question with what you've tried and we will go from there. – user692942 Nov 17 '21 at 11:23

3 Answers3

1

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.

user692942
  • 16,398
  • 7
  • 76
  • 175
  • 1
    I use relative paths with OpenTextFile() all the time. I just ensure that the current directory is set to the script directory and precede the filename with ".\". – LesFerch Nov 17 '21 at 18:53
  • @LesFerch in that case leave an [answer here](https://stackoverflow.com/q/15621395/692942) so others can benefit from your wisdom. I haven't tested it myself and the documentation makes no reference to relative path usage, seems strange that if it supports it why include a method called `GetAbsolutePathName()` in the `FileSystemObject` in the first place? In any case, you're still specifying a path modifier so it's along a similar line. In fact, it probably uses `GetAbsolutePathName()` in `OpenTextFile()`. – user692942 Nov 17 '21 at 19:12
  • @LesFerch suggested leaving an answer on [the original question](https://stackoverflow.com/q/15621395/692942) about relative paths, would have been better served there instead of [posting here](https://stackoverflow.com/a/70013922/692942). – user692942 Nov 19 '21 at 12:35
  • 1
    I posted that answer as requested and posted a new answer here which I think (hope) is more along the lines of what the OP really wants. – LesFerch Nov 20 '21 at 03:49
0

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.

user692942
  • 16,398
  • 7
  • 76
  • 175
  • you are wrong. File in the same folder with VBS – sogohimo Nov 16 '21 at 14:48
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – xTrimy Nov 16 '21 at 16:09
  • @sogohimo Craig is right, they just don't explain themselves very well. – user692942 Nov 17 '21 at 09:58
0

In bginfo, click Custom, New, enter ID for the identifier, Check the VB Script file radio button, and then click Browse to select a VBScript file you have saved with the code below. Then add the "ID" item to the bginfo display area. Note: The "file not found" error is resolved by using the AppData environment variable to reference the AnyDesk data file.

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

Please note that the data is returned to bginfo via one or more Echo statements. If testing this script outside of bginfo, change Echo to WScript.Echo.

LesFerch
  • 1,540
  • 2
  • 5
  • 21
  • Not a fan of magic numbers please define constants properly, in this case use `Const ForReading = 1` and `oFSO.OpenTextFile(DataFile, ForReading)` (even though it's the default). – user692942 Nov 18 '21 at 09:38
  • Has anyone ever managed to do something like this? Please show me a working example – sogohimo Nov 18 '21 at 12:32
  • Are you asking abut extracting the ID from the line "ad.annnet.id = 564654068"? That's a trivial addition of one line using **Split**. No need for regexp at all. I have updated the answer accordingly. If I've misunderstood, please elaborate on your question. – LesFerch Nov 18 '21 at 12:49
  • @sogohimo The question was about the error `File Not Found` if you are having issues with parsing the value from the file, ask a different question. But please, be clear descriptive and show what you have tried. – user692942 Nov 19 '21 at 12:33
  • @user692942 I have a remote access program installed. I need to display the user ID in bginfo. I'm don't know much about vbs, so I turned to stackoverflow. The system file is located along the path: C: \ Users \ sogohimo \ AppData \ Roaming \ AnyDesk \ system.conf File contents: ad.ancl.cached_config= ad.anynet.alias= ad.anynet.client_stats_hash= ad.anynet.cur_version= ad.anynet.fpr= ad.anynet.id=303314931 ad.anynet.last_relay= ad.anynet.network_hash= ad.anynet.network_id=main ad.anynet.relay.fatal_result=1.0 ad.anynet.relay.state=0 I need to display "303314931" – sogohimo Nov 19 '21 at 17:50
  • If you could help, could you send a letter to the post office? sogohimo@icloud.com – sogohimo Nov 19 '21 at 17:53
  • With bginfo, you can define new fields via a script, as described here https://www.verboon.info/2012/02/how-to-use-vbscripts-in-bginfo/ and you can also directly edit the bginfo configuration file via a script, since it's just a text file. Now that I have a better idea about what you're trying to, I'll edit my reply with something closer to what you're requesting. – LesFerch Nov 20 '21 at 03:01
  • @sogohimo Did this answer help you? – LesFerch Nov 24 '21 at 01:21