2016-02-22 132 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。網上還有大量的其他資源將參數傳遞給腳本並寫入文件。 –

相關問題