将VBA转换为从文本文件中删除PCL控制字符的VBS功能

时间:2019-06-17 20:27:19

标签: vba vbscript

我具有VBA功能,可以逐字符读取文本文件并删除PCL特殊控制字符。

我正打算将此功能转换为VBS代码,但由于某些原因,它无法按预期工作。

作为输入,我正在加载Data.txt文件,该文件包含文本行和某些特定的PCL字符。

作为输出文件,我将保留Data.txt文件的内容,而无需特殊的PCL字符控件。

这是VBA功能:

Function ConvertToDOS(InFile As String, OutFile As String, Optional FlagFormFeeds As Boolean = True) As Long
    On Error GoTo Error_ConvertToDOS

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Converts file into a DOS format and kills any unwanted characters
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim OneCharIn As String
    Dim LineDataIn As String
    Dim FF As String
    Dim LF As String


    Open InFile For Input As #1
    Open OutFile For Output As #2

    Print #2,
    LineDataIn = " "
    LF = Chr(10)
    FF = Chr(12)

    Do Until EOF(1)
        OneCharIn = Input(1, #1)    ' Reads in one Character at a time
        If (Asc(OneCharIn) >= 27) And (Asc(OneCharIn) < 125) Then
            LineDataIn = LineDataIn + OneCharIn
        ElseIf OneCharIn = LF Then
            Print #2, LineDataIn
            LineDataIn = ""
        ElseIf OneCharIn = FF Then
            If FlagFormFeeds Then
                Print #2, "EOF" & FF
            Else
                Print #2, FF
            End If
            LineDataIn = ""
        End If
    Loop

    Close #1     'Close Input File
    Close #2     'Close Output File

    ConvertToDOS = 0                       'Return 0 upon success
    Exit Function
Error_ConvertToDOS:
    ConvertToDOS = 1                        'Return 1 upon failure
    MDCReportError ("Error in ConvertToDOS() ->" & CStr(Err.Number) & " : " & Err.Description), False
End Function

这是我的VBS无法正常工作的功能:

Option Explicit

Const ForReading = 1

Dim InFile, OutFile, FlagFormFeeds, xBaseDataPath

xBaseDataPath = Watch.GetVariable("xBaseDataPath")

' Set path to Preprocessed.txt data file inside FilesTemp/ folder
InFile = xBaseDataPath & "\FilesTemp\Data.txt"

' Set path to Preprocessed.txt data file inside FilesTemp/ folder
OutFile = xBaseDataPath & "\FilesTemp\Data2.txt"

FlagFormFeeds = True

Call ConvertToDOS(InFile, OutFile, FlagFormFeeds)

' function
Function ConvertToDOS(InFile, OutFile, FlagFormFeeds)

    Dim OneCharIn, LineDataIn, FF, LF, inFSO, outFSO, currentFile, singleChar, outLine, data2File

    Set inFSO = CreateObject("Scripting.FileSystemObject")
    Set outFSO = CreateObject("Scripting.FileSystemObject")

    Set data2File = outFSO.CreateTextFile(OutFile, True)

    LineDataIn = " "
    LF = Chr(10)
    FF = Chr(12)

    Set currentFile = inFSO.OpenTextFile(InFile, ForReading)

    Do Until currentFile.AtEndOfStream
        singleChar = currentFile.Read(1)

        If (Asc(singleChar) >= 27) And (Asc(singleChar) < 125) Then
            LineDataIn = LineDataIn + singleChar
        ElseIf singleChar = LF Then
            data2File.Write LineDataIn & vbCRLF
            LineDataIn = ""
        ElseIf singleChar = FF Then
            If FlagFormFeeds Then
                data2File.Write "EOF" & FF
            Else
                data2File.Write FF
            End If
            LineDataIn = ""
        End If
    Loop

    data2File.Close
    currentFile.Close
    Set inFSO = Nothing
    Set outFSO = Nothing
End Function

问题在于,没有这样的特殊字符,例如(s17H已被删除。

我认为这很容易,但是对我来说太晚了。

感谢所有建议。

0 个答案:

没有答案