Error using VBScript in BGInfo File not found error - regex

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

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.

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.

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.

Related

How do I list files and directories those are not hidden in current directory using crystal language?

I wrote my own minimal version of "ls" command (Linux) using crystal language and here is my code:
require "dir"
require "file"
def main()
pwd = Dir.current
list_dir = Dir.children(pwd)
puts("[+] location: #{pwd}")
puts("------------------------------------------")
list_dir.each do |line|
check = File.file?(line)
if check == true
puts("[+] file : #{line}")
elsif check == false
puts("[+] directory: #{line}")
else
puts("[+] unknown : #{line}")
end
end
end
main
It works but it also listing all hidden files and directories (.files & .directories) too and I do not want to show those. I want the result more like "ls -l" command's result not like "ls -la".
So, what I need to implement to stop showing hidden files and directories?
There is nothing special about "hidden" files. It's just a convention to hide file names starting with a dot in some contexts by default. Dir.children does not follow that convention and expects the user to apply approriate filtering.
The recommended way to check if a file name starts with a dot is file_name.starts_with?(".").

I am writing a DXL script to print a modulename with path using a function . I need to remove if the directory has remote in that path

Below module is forming a directory from a path
Checkmodules("Cus", "projectname".cfg", ".*(Cus|Cer Requirements|Cer Speci|ASM|/ASM24e|Specs).*");
The above line forms below folder directory, so I need to modify it when the directory has remote in the path its should be removed entire directory.
Output path
Cus/spec/cer/
cus/val_remote/value - this also needs to be removed
Cus/sys/remote ---- to be removed entire path
You can do a replace (to an empty string) with the following regex combined with the flag g (global=all occurrences) and m (multiline)
^.*remote.*$
In JavaScript this would be :
outputPath.replace(/^.*remote.*$/gm, '')

Python: Returning a filename for matching a specific condition

import sys, hashlib
import os
inputFile = 'C:\Users\User\Desktop\hashes.txt'
sourceDir = 'C:\Users\User\Desktop\Test Directory'
hashMatch = False
for root, dirs, files in os.walk(sourceDir):
for filename in files:
sourceDirHashes = hashlib.md5(filename)
for digest in inputFile:
if sourceDirHashes.hexdigest() == digest:
hashMatch = True
break
if hashMatch:
print str(filename)
else:
print 'hash not found'
Contents of inputFile =
2899ebdb5f7a90a216e97b3187851fc1
54c177418615a90a6424cb945f7a6aec
dd18bf3a8e0a2a3e53e2661c7fb53534
Contents of sourceDir files =
test
test 1
test 2
I almost have the code working, I'm just tripping up somewhere. My current code that I have posted always returns the else statement, that the hash hasn't been found, even although they do as I have verified this. I have provided the content of my sourceDir so that someone case try this, the file names are test, test 1 and test 2, the same content is in the files.
I must add however, I am not looking for the script to print the actual file content, but rather the name of the file.
Could anyone suggest to where I am going wrong and why it is saying the condition is false?
You need to open the inputFile using open(inputFile, 'rt') then you can read the hashes. Also when you do read the hashes make sure you strip them first to get rid of new line characters \n at the end of the lines

If EXE opens, then RUN

I need a script that. If exe opens, then URL will open. And I did make a shortcut for the URL.
I found this script, on stack overflow, and was going to use it changing the arguments of course, but I figured there would be a simpler way
EDIT: If League of Legends.exe opens [This is the client itself], then run C:..\KSD.url
Option Explicit
Private Const Folder As String = "c:\windows\system32\foldername"
Private Const FileToRun As String = "\\servername\folder\software.exe"
Sub Run(ByVal sFile)
Dim shell
Set shell = CreateObject("WScript.Shell")
shell.Run Chr(34) & sFile & Chr(34), 1, False
Set shell = Nothing
End Sub
Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
If Not fs.FolderExists(Folder) Then
Run FileToRun
End If
Try Exec : http://msdn.microsoft.com/en-us/library/ateytk4a(v=vs.84).aspx
objExec = shell.Exec Chr(34) & sFile & Chr(34)
if objExec.Status = 0 then ' your program is running
' open your url
end if
You may need error handling if your file does not open (see MSDN documenation above). Please code responsibly :-)

QFileDialog: adding extension automatically when saving file?

When using a QFileDialog to save a file and to specify the extension (like *.pdf) and the user types in a name without this extension, also the saved file hasn't this extension.
Example-Code:
QFileDialog fileDialog(this, "Choose file to save");
fileDialog.setNameFilter("PDF-Files (*.pdf)");
fileDialog.exec();
QFile pdfFile(fileDialog.selectedFiles().first());
now when the user enters "foo" as the name, the file will be saved as "foo", not as "foo.pdf". So the QFileDialog doesn't add the extension automatically. My question: How can I change this?
You could use QFileDialog::setDefaultSuffix():
This property holds suffix added to the filename if no other suffix was specified.
This property specifies a string that will be added to the filename if it has no suffix already. The suffix is typically used to indicate the file type (e.g. "txt" indicates a text file).
For multiple file filters, the following can be done.
import re
import os
def saveFile(self):
path, fileFilter = QFileDialog().getSaveFileName(self, "Save file",
"", "Gnuplot Files (*.plt)"
+ ";;" + "Gnuplot Files (*.gp)"
+ ";;" + "Gnuplot Files (*.gpt)"
+ ";;" + "Text Files (*.txt)")
selectedExt = re.search('\((.+?)\)',fileFilter).group(1).replace('*','')
# Attach extension as per selected filter,
# if file does not have extension.
if not os.path.splitext(path)[1]:
path = path + selectedExt
print(path)