
Announcements
Hi Team,
I am vb script action which reads the json file and extract the numbers from it.
and store it in an array then finding largest number from that array.
now i want to output this value in my desktop variable.
Also I am passing the json file path via desktop variable.
set variable LargestNum.
here is my script
' Create a FileSystemObject
Set fs = CreateObject("Scripting.FileSystemObject")
' Define the path to your JSON file
Dim filePath
filePath = %JsonFilePath% ' Replace with your actual file path
' Array to store extracted numbers
Dim numbers()
ReDim numbers(-1)
' Check if the file exists
If fs.FileExists(filePath) Then
' Read JSON content from the file
Dim jsonContent
Set file = fs.OpenTextFile(filePath, 1) ' 1: ForReading
jsonContent = file.ReadAll()
file.Close
' Regular expression pattern to extract all numbers followed by a colon
Dim regexPattern
regexPattern = """(\d+)""\s*:"
' Create a RegExp object
Dim regex
Set regex = New RegExp
' Set properties for the RegExp object
regex.Global = True ' Match all occurrences
regex.IgnoreCase = True
regex.MultiLine = True
regex.Pattern = regexPattern
' Execute the regular expression on the JSON string
Dim matches
Set matches = regex.Execute(jsonContent)
' Check if matches are found
If matches.Count > 0 Then
' Resize the array to hold all matches
ReDim numbers(matches.Count - 1)
' Iterate over each match
For i = 0 To matches.Count - 1
' Extract the matched number (from the first capturing group)
numbers(i) = CInt(matches(i).SubMatches(0)) ' Convert to integer
Next
' Find the largest number in the array
Dim largestNumber
largestNumber = numbers(0) ' Initialize with the first element
For i = 1 To UBound(numbers)
If numbers(i) > largestNumber Then
largestNumber = numbers(i)
End If
Next
' Print the largest number found
WScript.Echo "Largest number: " & largestNumber
%LargestNumber% = largestNumber
Else
WScript.Echo "No numbers followed by colon found in the JSON."
End If
Else
WScript.Echo "File not found: " & filePath
End If
Please correct it .
Thanks in advance!!!
I think your line 61 is incorrect: %LargestNumber% = largestNumber
You cant set PAD variable inside vbscript. You only use WScript.Echo to output values from VBscript. Then you can use trim, split, replace or just set variable in PAD after vbscript has outputted some values.
So something like this:
So if Larges number is found and its 98 VBScriptOutputs is: "Largest number: 98"
if no numbers followed by colon is found it will produce this:
Also you might need to have double quotes around filepath variable (or have the double quotes in the JsonFilePath variable around the file path):
Tip:
In your VBscript action have ScriptError variable enabled so it produces info on error:
Then if it gives some text it should look something like this:
That means that in row 6 there is error (so in my try it was file path not correct form).