-3

I need to help in converting a Python script to VBScript. I'm trying to load the .cal file as a binary value file and edit a particular value in the file but unfortunately, my environment only supports VBScript.

import argparse

parser = argparse.ArgumentParser(description='Sapix Cal File Sensitivity Adjustment')
parser.add_argument("-calfile", default="test.cal", help="Enter the Calfile name (ex: 09781DK5081.cal")
parser.add_argument("-vtest", default=125, help="New Vtest setting (85-205)")
parser.add_argument("-vref", default=250, help="New Vref setting (250-120)")


args = parser.parse_args()
calfile = args.calfile
vtest = args.vtest
vref = args.vref


print(calfile)
print(vtest)
print(vref)


with open(calfile, "rb") as binary_file:
    # Read the whole file at once
    data = bytearray(binary_file.read())

    # Find Line with VTEST setting
    ivteststart = data.find(bytearray('PARALLEL_VOLTAGE_TEST', 'utf-8'))
    ivtestend = data.find(b'\n',ivteststart)

    # Remove original VTEST line
    del data[ivteststart:ivtestend+1]

    # Insert New Line with new VTEST
    new_vtest = bytearray("PARALLEL_VOLTAGE_TEST %s\n" % (vtest),'utf-8')
    data[ivteststart:ivteststart] = new_vtest

    # Find Line with VREF setting
    ivrefstart = data.find(bytearray('PARALLEL_VOLTAGE_REF', 'utf-8'))
    ivrefend = data.find(b'\n',ivrefstart)

    # Remove original VREF line
    del data[ivrefstart:ivrefend+1]

    # Insert New Line with new VREF
    new_vref = bytearray("PARALLEL_VOLTAGE_REF %s\n" % (vref),'utf-8')
    data[ivrefstart:ivrefstart] = new_vref



    # Write new sensitivity settings to cal file
with open(calfile, "wb") as binary_file:
    binary_file.write(data)

I was able to make the changes if I load the file as text file but I have no clue how to load the same as Binary value and make the changes

Option Explicit

Dim objFso, objFolder, objFile, objOtF, cd, content

Dim targetDir
targetDir = "C:\Kiosk\UI"

Dim objRegExp
Set objRegExp = New RegExp
objRegExp.Pattern = "\bPARALLEL_VOLTAGE_TEST \d+\b"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(targetDir)

For Each objFile in objFolder.Files
    If LCase(Right(objFile.Name, 4)) = ".cal" Then
        cd = objFile.Name

        Set objOtF = objFso.OpenTextFile(cd, 1)
        content = objOtF.ReadAll
        objOtF.Close

        Set objOtF = objFso.OpenTextFile(cd, 2)
        objOtF.Write objRegExp.Replace(content, "PARALLEL_VOLTAGE_TEST 230")
        objOtF.Close


Dim objRegExp1
Set objRegExp1 = New RegExp
objRegExp1.Pattern = "\bPARALLEL_VOLTAGE_REF \d+\b"

        Set objOtF = objFso.OpenTextFile(cd, 1)
        content = objOtF.ReadAll
        objOtF.Close

        Set objOtF = objFso.OpenTextFile(cd, 2)
        objOtF.Write objRegExp1.Replace(content, "PARALLEL_VOLTAGE_REF 190")
        objOtF.Close

    End If
    Next
Étienne Laneville
  • 4,697
  • 5
  • 13
  • 29

1 Answers1

0

Take a look at the following post: Read and write binary file in VBscript. You might be able to use ADODB.Stream to read and write binary data. Other approaches are explored also, including reading characters one by one into an array.

Here's the code from that post:

Function readBinary(strPath)

    Dim oFSO: Set oFSO = CreateObject("Scripting.FileSystemObject")
    Dim oFile: Set oFile = oFSO.GetFile(strPath)

    If IsNull(oFile) Then MsgBox("File not found: " & strPath) : Exit Function

    With oFile.OpenAsTextStream()
        readBinary = .Read(oFile.Size)
        .Close
    End With

End Function

Function writeBinary(strBinary, strPath)

    Dim oFSO: Set oFSO = CreateObject("Scripting.FileSystemObject")

    ' below lines pupose: checks that write access is possible!
    Dim oTxtStream

    On Error Resume Next
    Set oTxtStream = oFSO.createTextFile(strPath)

    If Err.number <> 0 Then MsgBox(Err.message) : Exit Function
    On Error GoTo 0

    Set oTxtStream = Nothing
    ' end check of write access

    With oFSO.createTextFile(strPath)
        .Write(strBinary)
        .Close
    End With

End Function
Étienne Laneville
  • 4,697
  • 5
  • 13
  • 29