Manipulating data via VBScript

I am trying to use VBScript to manipulate data to and from .TXT files

I can successfully save information to a .TXT file, in a '$TagName; TagValue' kind of format.

I seem to be able to read that information back into variables, but I am struggling to use this information.

-----

Dim fso, File, FileValue, tagName, tagValue, strLine, arrItems, objFile, objTag

FileValue = $Seq_Name
$Z = 1
Set fso = CreateObject("Scripting.FileSystemObject")
$Z = 2
File = (FileValue & ".txt")
$Z = 3
If fso.FileExists(File) Then
$Z = 4
$Y = File
$Z = 5
objFile = fso.OpenTextFile(File,1)
$Z = 6
Do
$Z = 7
strLine = objFile.ReadLine
arrItems = Split(strLine, ";", -1, 1)
tagName = CStr(arrItems(0))
tagValue = CStr(arrItems(1))
$Z = 8
Set objTag = TagName
$Z = 9
objTag.Value = tagValue
$Z = 10
objTag.Write
$Z = 11
Loop Until objFile.AtEndOfStream
$Z = 12
End If
objFile.Close

-----

$Z and $Y are my trace tags.  The script gets to '$Z = 8' then stops.  This is where i need to identify the contents of TagName as a tag, and then copy information into it.

Any pointers would be appreciated.

Parents
  • Hi Bob, I don't know if you still need help with this, but I have a few thoughts to make.

    First, when you need to interact with project tags in your script sheet, it is recommended to use the built-in "GetTagValue" and "SetTagValue" functions, as they increase readability and make it easier to understand whether we are using vbscript tags or project tags.

    Second, you can use the built-in function "Trace" to trace your messages in the LogWin instead of using project tags like "Y" and "Z". It's possible to write any text and concatenate project and script tags. And you also don't spend tags just to "Trace", that can help.

    Third, it's possible to use the Debug mode in VBScript, this can help you identify where your code is failing, it's also useful.

    Now let's talk about the script you're using, if I understood correctly, your txt file has two values: The first string of the line is the project tag name, and the string after the semicolon is the value you want to assign to the tag.

    I used this script in my application and it's working to achieve this goal:

    Dim filePath
    filePath = "your_path_here"
    
    ' Open the text file for reading
    Dim fso, file, line, values, tagName, tagValue
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set file = fso.OpenTextFile(filePath, 1)
    
    $Trace("txt open")
    
    ' Loop through each line in the file
    Do Until file.AtEndOfStream
    	line = file.ReadLine()
    	$Trace("Line read")
        
        ' Split the line by the semicolon separator
    	values = Split(line, ";")
        
        ' Ensure there are at least 2 parts (before and after the semicolon)
    	If UBound(values) >= 1 Then
    		tagName = Trim(values(0))  ' First value (before the semicolon)
    		tagValue = Trim(values(1))  ' Second value (after the semicolon)
            
            ' Output the values enclosed in VBScript tags
            $Trace("The first value is" & tagName & " and the second value is" & tagValue)
            
            $SetTagValue(tagName, tagValue)
            $Trace("Tag value set")
        End If
    Loop
    
    ' Close the file
    file.Close
    Set file = Nothing
    Set fso = Nothing
    
    $Trace("Finished process")


    Also, remember that if you're reading and writing a huge number of data, you can use Python in AVEVA Edge 2023 R2. Using Python for data manipulation from and to a file is superior to VBScript due to Python's extensive libraries like pandas and csv, which provide optimized data structures and built-in functions for efficient file operations. Python also supports a wide range of file formats (CSV, JSON, XML, etc.), enables exception handling with try-except blocks, and offers better performance due to its modern execution engine. Additionally, Python's portability across different operating systems and its ability to handle large datasets efficiently make it more scalable than VBScript.

    I hope that helps you!

Reply
  • Hi Bob, I don't know if you still need help with this, but I have a few thoughts to make.

    First, when you need to interact with project tags in your script sheet, it is recommended to use the built-in "GetTagValue" and "SetTagValue" functions, as they increase readability and make it easier to understand whether we are using vbscript tags or project tags.

    Second, you can use the built-in function "Trace" to trace your messages in the LogWin instead of using project tags like "Y" and "Z". It's possible to write any text and concatenate project and script tags. And you also don't spend tags just to "Trace", that can help.

    Third, it's possible to use the Debug mode in VBScript, this can help you identify where your code is failing, it's also useful.

    Now let's talk about the script you're using, if I understood correctly, your txt file has two values: The first string of the line is the project tag name, and the string after the semicolon is the value you want to assign to the tag.

    I used this script in my application and it's working to achieve this goal:

    Dim filePath
    filePath = "your_path_here"
    
    ' Open the text file for reading
    Dim fso, file, line, values, tagName, tagValue
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set file = fso.OpenTextFile(filePath, 1)
    
    $Trace("txt open")
    
    ' Loop through each line in the file
    Do Until file.AtEndOfStream
    	line = file.ReadLine()
    	$Trace("Line read")
        
        ' Split the line by the semicolon separator
    	values = Split(line, ";")
        
        ' Ensure there are at least 2 parts (before and after the semicolon)
    	If UBound(values) >= 1 Then
    		tagName = Trim(values(0))  ' First value (before the semicolon)
    		tagValue = Trim(values(1))  ' Second value (after the semicolon)
            
            ' Output the values enclosed in VBScript tags
            $Trace("The first value is" & tagName & " and the second value is" & tagValue)
            
            $SetTagValue(tagName, tagValue)
            $Trace("Tag value set")
        End If
    Loop
    
    ' Close the file
    file.Close
    Set file = Nothing
    Set fso = Nothing
    
    $Trace("Finished process")


    Also, remember that if you're reading and writing a huge number of data, you can use Python in AVEVA Edge 2023 R2. Using Python for data manipulation from and to a file is superior to VBScript due to Python's extensive libraries like pandas and csv, which provide optimized data structures and built-in functions for efficient file operations. Python also supports a wide range of file formats (CSV, JSON, XML, etc.), enables exception handling with try-except blocks, and offers better performance due to its modern execution engine. Additionally, Python's portability across different operating systems and its ability to handle large datasets efficiently make it more scalable than VBScript.

    I hope that helps you!

Children
No Data