【问题标题】:VB Script to extract text from file using delimitersVB脚本使用分隔符从文件中提取文本
【发布时间】:2016-06-03 19:54:07
【问题描述】:

我尝试使用批处理来提取所需的代码,但这不适用于大文件。我想知道这是否可以使用 VB 脚本。所以,

我需要从两个分隔符之间的文件中提取文本并将其复制到 TXT 文件。此文本看起来像 XML 代码,而不是分隔符 <string> text... </string>,我有 :::SOURCE text .... ::::SOURCE。正如您在第一个分隔符中看到的那样,是 ':' 的 3 倍,第二个是 ':' 的 4 倍

最重要的是这两个分隔符之间有多行。

文本示例:

text&compiled unreadable characters
text&compiled unreadable characters
:::SOURCE
just this code
just this code
...
just this code
::::SOURCE text&compiled unreadable characters
text&compiled unreadable characters

期望的输出:

just this code
just this code
...
just this code

【问题讨论】:

    标签: vbscript


    【解决方案1】:

    也许你可以试试这样的:

    filePath = "D:\Temp\test.txt"
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.OpenTextFile(filePath)
    
    startTag = ":::SOURCE"
    endTag = "::::SOURCE"
    startTagFound = false
    endTagFound = false
    outputStr = ""
    
    Do Until f.AtEndOfStream
        lineStr = f.ReadLine
        startTagPosition = InStr(lineStr, startTag)
        endTagPosition = InStr(lineStr, endTag)
    
        If (startTagFound) Then
            If (endTagPosition >= 1) Then
                outputStr = outputStr + Mid(lineStr, 1, endTagPosition - 1)
                Exit Do
            Else
                outputStr = outputStr + lineStr + vbCrlf
            End If
        ElseIf (startTagPosition >= 1) Then
            If (endTagPosition >= 1) Then
                outputStr = Mid(lineStr, startTagPosition + Len(startTag), endTagPosition - startTagPosition - Len(startTag) - 1)
                Exit Do
            Else
                startTagFound = true
                outputStr = Mid(lineStr, startTagPosition + Len(startTag)) + vbCrlf
            End If
        End If
    Loop
    
    WScript.Echo outputStr
    
    f.Close
    

    我假设开始和结束标记可以在文件内的任何位置,而不仅仅是在行首。如果您有更多关于“编码”的信息,也许您可​​以简化代码。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多