VBScript: Determining correct tristate value for text file to import
783124May 9 2012 — edited May 9 2012I realise this is more of a specific VBScript question, however I am trying to build a subroutine for the BefFileImport event script in FDM and am having a few issues. The CSV files output from the source system are of UCS-2 Little Endian encoding. FDM interprets these files rather oddly (loads of additional quotation marks, which need removing), and the numeric values in the data field are grouped by commas.
So I wanted to build a BefFileImport subroutine to pre-process the CSV files as follows:
Script:
Sub BefFileImport(strLoc, strCat, strPer, strFile)
' Declare local variables
Dim fso, f1, f2
Dim strTname, strFName
Dim strLine, strOut
Dim aD
' Declare file system Object
Set fso = CreateObject("Scripting.FileSystemObject")
' Open csv file for reading
Set f1 = fso.OpenTextFile(strFile, 1, False, -1)
' Create a temp file
strTName = Left(strFile,Len(strFile)-4) & ".TMP"
Set f2 = fso.CreateTextFile(strTName,True)
' Loop through columns of csv row
Do Until f1.AtEndOfStream
' Store line in a variable
strLine = f1.ReadLine
' Remove commas from values in Data field (assumed to be eighth, final field)
aD = Split(strLine,",")
For i = 0 To UBound(aD)
If i = 0 Then
strOut = aD(i)
ElseIf i < 8 Then
strOut = strOut & "," & aD(i)
Else
strOut = strOut & aD(i)
End If
Next
' Finally, remove any quotation characters and output
strOut = Replace(strOut,"""","")
2.WriteLine(strOut)
Loop
f2.Close
f1.Close
' Copy replace original file with processed on and clean up
Set f1 = fso.GetFile(strFile)
Set f2 = fso.GetFile(strTname)
f1.Delete
f2.Copy(strFile)
f2.Delete
Set fso = Nothing
End Sub
However, the problem comes when I try to run the script a second time. The problem lies in the following line of code:
Set f1 = fso.OpenTextFile(strTemp, 1, False, -1)
Error (Financial Data Management Workbench):
5 - Invalid procedure call or argument
At Line:58
Since the file has been re-saved as Ascii rather than Unicode, and it is still trying to open it as a Unicode file, it falls over. So, on the second and subsequent attempts, the line needs to be changed to:
Set f1 = fso.OpenTextFile(strTemp, 1, False, -2)
Is there a way to determine the file encoding beforehand, and act accordingly? Or is there a far more elegant solution here? I was hoping to re-save the original CSV file rather than create a second version, to save on hard disk space and reduce the potential file count in the Inbox.