Convert Unicode Files to ASCII

I had a situation at work recently where I needed to mine some Unicode-formatted HL7 files for data.  For my purposes, I really needed the files in plain text format.  Here’s the VBScript I used to convert the HL7 files to plain text.  I hope you find it useful.

Const ForReading = 1
Const ForWriting = 2

‘— The possible values for TriStateTrue are 0, -1, and -2.  -2 is the setting I needed in this case.
Const TriStateTrue = -2

Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objFile = objFSO.OpenTextFile(“C:\HL7Archive\000001.hl7″, ForReading,False,TriStateTrue)

strText = objFile.ReadAll
‘— In my case I needed to replace the linefeed character, chr(13), with VbCrLf.
‘— Otherwise the entire file would have ended up on one single line.
strLine = Replace(strText, chr(13), VbCrLf)
objFile.Close

‘— Now we have the properly formatted file in memory.  Let’s write it to a new text file.
Set objNewFile = objFSO.CreateTextFile(“C:\Documents and Settings\jharbert\Desktop\HL7Testing\gpa000000.txt”, True)
objNewFile.WriteLine strLine
objNewFile.Close

  • Anonymous

    Const ForReading = 1
    Const ForWriting = 2

    ‘— The possible values for TriStateTrue are 0, -1, and -2. -2 is the setting I needed in this case.
    Const TriStateTrue = -2

    Set objFSO = CreateObject(“Scripting.FileSystemObject”)
    Set objFile = objFSO.OpenTextFile(WScript.Arguments(0), ForReading,False,TriStateTrue)

    strText = objFile.ReadAll
    ‘— In my case I needed to replace the linefeed character, chr(13), with VbCrLf.
    ‘— Otherwise the entire file would have ended up on one single line.
    strLine = Replace(strText, chr(13), VbCrLf)
    objFile.Close

    ‘— Now we have the properly formatted file in memory. Let’s write it to a new text file.
    Set objNewFile = objFSO.CreateTextFile(WScript.Arguments(1), True)
    objNewFile.WriteLine strLine
    objNewFile.Close

  • Anonymous

    Const ForReading = 1
    Const ForWriting = 2

    ‘— The possible values for TriStateTrue are 0, -1, and -2. -2 is the setting I needed in this case.
    Const TriStateTrue = -2

    Set objFSO = CreateObject(“Scripting.FileSystemObject”)
    Set objFile = objFSO.OpenTextFile(WScript.Arguments(0), ForReading,False,TriStateTrue)

    strText = objFile.ReadAll
    ‘— In my case I needed to replace the linefeed character, chr(13), with VbCrLf.
    ‘— Otherwise the entire file would have ended up on one single line.
    strLine = Replace(strText, chr(13), VbCrLf)
    objFile.Close

    ‘— Now we have the properly formatted file in memory. Let’s write it to a new text file.
    Set objNewFile = objFSO.CreateTextFile(WScript.Arguments(1), True)
    objNewFile.WriteLine strLine
    objNewFile.Close

  • Michel de Ruiter

    According to http://msdn.microsoft.com/library/314cz14s.aspx, the value -2 is called TristateUseDefault, not TriStateTrue.