2016-02-22 131 views
1

我尝试使用批量来提取所需的代码,但这并不适用于大文件。我想知道这是可能的VB脚本。所以,VB脚本使用分隔符从文件中提取文本

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

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

例子:

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 

回答

1

也许你可以试试somethig这样的:

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 

我所做的假设开始和结束t ag可以位于文件的任何位置,不仅在行首。也许你可以简化代码,如果你有更多关于“编码”的信息。

+0

谢谢@Thomas,你是超级明星。你有没有任何想法如何我可以从批处理文件发送文件路径到此脚本并回显到文本文件?干杯,安迪 – Andy

+0

嗨@Andy,检查这些答案:http://stackoverflow.com/a/2806731/1123674和http://stackoverflow.com/a/34046444/1123674。网上还有大量的其他资源将参数传递给脚本并写入文件。 –

相关问题