Hi there
does anyone know how to convert the below VB to JS ?
i’m stuck around the LoadFileFrom and accessing the variables…
thanks in advance, any help will be appreciated
// write out …
Option Explicit
Dim MyMeta, x
Dim csv1
Dim ObjFSO, ObjFile
Dim UID, Addr1, Addr2, Addr3, Suburb, City, Postcode
dim allOK, doneHeader
'— create and open metadata
Set MyMeta = CreateObject(“MetaDataLib.MetaFile”)
MyMeta.LoadFromFile(Watch.GetMetadataFileName)
csv1 = Trim(Watch.GetVariable(“csv1”))
Watch.Log “csv1=” & csv1,4
’ create and open text file
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objFile = objFSO.CreateTextFile(csv1)
allOK = true
doneHeader = false
'— loop through and read in the metadata fields for each document
For x = 1 to MyMeta.Job.Group(0).Count
UID = CStr(x-1)
Addr1 = “”
Addr2 = “”
Addr3 = “”
Suburb = “”
City = “”
Postcode = “”
if doneHeader = false then
'--- write out headers, using data tools field names
'--- using "|" instead of vbTab
objFile.WriteLine(chr(34) & "UID" & chr(34) & "," & chr(34) & "Address Line 1" & chr(34) & "," & chr(34) & "Address Line 2" & chr(34) & "," & chr(34) & "Address Line 3" & chr(34) & "," & chr(34) & "Suburb" & chr(34) & "," & chr(34) & "City" & chr(34) & "," & chr(34) & "Postcode" & chr(34))
doneHeader = true
end if
Addr1 = Trim(MyMeta.Job.Group(0).Document(x-1).FieldByName("_vger_fld_postal_address_line1"))
Watch.Log Addr1,4
Addr2 = Trim(MyMeta.Job.Group(0).Document(x-1).FieldByName("_vger_fld_postal_address_line2"))
Addr3 = Trim(MyMeta.Job.Group(0).Document(x-1).FieldByName("_vger_fld_postal_address_line3"))
Suburb = Trim(MyMeta.Job.Group(0).Document(x-1).FieldByName("_vger_fld_postal_suburb"))
City = Trim(MyMeta.Job.Group(0).Document(x-1).FieldByName("_vger_fld_postal_town_city"))
Postcode = Trim(MyMeta.Job.Group(0).Document(x-1).FieldByName("_vger_fld_postal_postcode"))
objFile.WriteLine(chr(34) & UID & chr(34) & "," & chr(34) & Addr1 & chr(34) & "," & chr(34) & Addr2 & chr(34) & "," & chr(34) & Addr3 & chr(34) & "," & chr(34) & Suburb & chr(34) & "," & chr(34) & City & chr(34) & "," & chr(34) & Postcode & chr(34))
Next
objFile.Close
?? read in
'— apply the DPID, SortPlan fields to the Metadata
'— NOTE: the UID field is the document index
Option Explicit
Dim MyMeta, myCollection
Dim myPath, myFile, myLine, myFields, myUID, myDPID, mySortPlan, myBarcode, myPieceID
Dim ObjFSO, ObjFile
Dim mySort, myKey, x, myReason
Dim myA1, myA2, myA3, myS, myC, myPC
'— open metadata
Set MyMeta = CreateObject(“MetaDataLib.MetaFile”)
MyMeta.LoadFromFile(Watch.GetMetadataFileName)
'— get filename and path
myFile = Trim(Watch.GetVariable(“csv2”))
'— open the IN file
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objFile = objFSO.OpenTextFile(myFile)
Watch.Log “opened–>” & myFile,4
'— loop thru the file assigning the DPID, Barcode to the metadata
'— based on the document index (UID)
myLine = objFile.ReadLine '— bypass field names on first line
myLine = “”
Do While Not objFile.AtEndOfStream
myLine = objFile.ReadLine
myFields = CSVParse(myLine)
'-- InputAddress Line 1,InputAddress Line 2,InputAddress Line 3,InputSuburb,InputCity,InputPostcode,ReferenceUID,OutputNew Field,OutputNew Field 2,OutputNew Field 3,OutputNew Field 4,OutputNew Field 5,OutputNew Field 6,OutputNew Field 7
myUID = CInt(myFields(6))
myA1 = myFields(7)
myA2 = myFields(8)
myA3 = myFields(9)
myS = myFields(10)
myC = myFields(11)
myPC = myFields(12)
myDPID = myFields(13)
MyMeta.Job.Group(0).Document(myUID).Fields.Add "_vger_fld_DPID", myDPID
MyMeta.Job.Group(0).Document(myUID).Fields.Add "_vger_fld_new_address1",myA1
MyMeta.Job.Group(0).Document(myUID).Fields.Add "_vger_fld_new_address2",myA2
MyMeta.Job.Group(0).Document(myUID).Fields.Add "_vger_fld_new_address3",myA3
MyMeta.Job.Group(0).Document(myUID).Fields.Add "_vger_fld_new_suburb",myS
MyMeta.Job.Group(0).Document(myUID).Fields.Add "_vger_fld_new_city",myC
MyMeta.Job.Group(0).Document(myUID).Fields.Add "_vger_fld_new_postcode",myPC
Loop
'— and close the OUT file
objFile.Close
'— andf save metadata
MyMeta.SaveToFile(Watch.GetMetadataFileName)
'— helper csv function
Function CSVParse(ByVal strLine)
’ Function to parse comma delimited line and return array
’ of field values.
Dim arrFields
Dim blnIgnore
Dim intFieldCount
Dim intCursor
Dim intStart
Dim strChar
Dim strValue
Const QUOTE = """"
Const QUOTE2 = """"""
' Check for empty string and return empty array.
If (Len(Trim(strLine)) = 0) then
CSVParse = Array()
Exit Function
End If
' Initialize.
blnIgnore = False
intFieldCount = 0
intStart = 1
arrFields = Array()
' Add "," to delimit the last field.
strLine = strLine & ","
' Walk the string.
For intCursor = 1 To Len(strLine)
' Get a character.
strChar = Mid(strLine, intCursor, 1)
Select Case strChar
Case QUOTE
' Toggle the ignore flag.
blnIgnore = Not blnIgnore
Case ","
If Not blnIgnore Then
' Add element to the array.
ReDim Preserve arrFields(intFieldCount)
' Makes sure the "field" has a non-zero length.
If (intCursor - intStart > 0) Then
' Extract the field value.
strValue = Mid(strLine, intStart, _
intCursor - intStart)
' If it's a quoted string, use Mid to
' remove outer quotes and replace inner
' doubled quotes with single.
If (Left(strValue, 1) = QUOTE) Then
arrFields(intFieldCount) = _
Replace(Mid(strValue, 2, _
Len(strValue) - 2), QUOTE2, QUOTE)
Else
arrFields(intFieldCount) = strValue
End If
Else
' An empty field is an empty array element.
arrFields(intFieldCount) = Empty
End If
' increment for next field.
intFieldCount = intFieldCount + 1
intStart = intCursor + 1
End If
End Select
Next
' Return the array.
CSVParse = arrFields
End Function