VBScript如何将vbCrLF字符存储在已解析的字符串数组中?

时间:2015-11-30 00:21:32

标签: arrays parsing vbscript

打开一个对话框以选择输入txt文件后,我的脚本将使用Split函数将此文件解析为由空格分隔的字符串数组。然后我循环遍历此数组的每个元素,并根据情况执行两个操作之一(使用Switch语句)。目的是打印,直到遇到某个字符串(“Created:”)然后什么都不做,直到我连续遇到4个vbCrLF字符(此时我将再次开始打印,直到遇到前面提到的字符串) 。该脚本永远无法按顺序检测这4个字符。即使我试图检索数组索引的值,我期望一个独立的vbCrLF,我发现它在数组中根本不存在。如果这些实例未存储为已解析数组的元素,我该如何找到它们?

我无法提供输入txt文件,但提前给任何可以帮助解决此问题的人。

代码:

(PS忽略了数组循环存在'下标超出范围'错误的事实;这很容易修复)

dim wShell, oExec, objFSO, fIn, fOut, fTest

'Open a dialog box and select the input text file

Set wShell = CreateObject("WScript.Shell")
Set oExec = wShell.Exec("mshta.exe ""about:<input type=file id=FILE><script>FILE.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);</script>""")
txtInFileSelected = oExec.StdOut.ReadLine

'Create object for the input text file
Set objFSO = CreateObject("scripting.filesystemobject")
Set fIn = objFSO.OpenTextFile(txtInFileSelected)


'Read in file & close
strData = fIn.ReadAll
fIn.Close
Set fIn = Nothing

'Parse items into array
arrItems = Split(strData)


isItem = 1

'Gather important input data and print in a pleasant format on the output file 
For i = 0 to Ubound(arrItems)




    Select Case isItem

        Case 1
            wscript.echo arrItems(i)
            If arrItems(i+1) <> "Created:" Then
            isItem = 1
            Else 
            isItem = 0
            End If

        Case 0

            If Instr(arrItems(i), vbCr) = 1 And Instr(arrItems(i), vbLF) = 2 And Instr(arrItems(i+1), vbCr) = 1 And Instr(arrItems(i+1), vbLF) = 2 And Instr(arrItems(i+2), vbCr) = 1 And Instr(arrItems(i+2), vbLF) = 2 And Instr(arrItems(i+3), vbCr) = 1 And Instr(arrItems(i+3), vbLF) = 2 Then
            isItem = 1              
            Else 
            isItem = 0

            End If



        End Select



Next

1 个答案:

答案 0 :(得分:0)

尝试按以下方式拆分数据:

BlockTxtArry = Split(strData,vbCRLF & vbCRLF & vbCRLF & vbCRLF)

For Each BlockTxt in BlockTxtArry
    HalfBlock = Split(BlockTxt,"Created:")  
    TxtIwant = HalfBlock(0)  'Section starting with 4x vbcrlf and ending with "Created:"
    TxtIwant = TxtIwant & vbcrlf & "=============" & vbcrlf 
    MsgBox TxtIwant
    AllTxtIwant = AllTxtIwant & vbcrlf & TxtIwant
Next 

MsgBox AllTxtIwant