2016-05-12 92 views
-2

我想創建一個VBscript文件,將文本文件分隔成多個文本文件。我在一段時間內沒有做任何編程,而且我一直在爲此敲打我的頭幾天。VB腳本將文本分成多個文件

這是文本文件的一部分。

Tested on,8 May 2016,,,, 
Asset ID,126567,,,, 
Rigel 288,Z48-0366,,,, 
Site,Workshop,,,, 
Location,WORKSHOP,,,, 
AP Setup,,0,,, 
User Name,Workshop,,,, 
Test Sequence,TestCode-BGC2,,,, 
Live Voltage,,, 248,,,V 
Neutral Voltage,,, 2,,,V 
Load Current,,, 0.0,,,A 
Load Test,,, 0.0,,,kVA 
Enclosure Lkg,Mains Normal,, 8,Pass,100,µA 
Enclosure Lkg,Mains Normal,SFC: Neutral Open, 12,Pass,500,µA 
Enclosure Lkg,Mains Reversed,, 8,Pass,100,µA 
Enclosure Lkg,Mains Reversed,SFC: Neutral Open, 12,Pass,500,µA 
User Comment,,,, 
Status,Pass 

Tested on,8 May 2016,,,, 
Asset ID,126563,,,, 
Rigel 288,Z48-0366,,,, 
Site,Workshop,,,, 
Location,WORKSHOP,,,, 
AP Setup,,0,,, 
User Name,Workshop,,,, 
Test Sequence,TestCode-BGC2,,,, 
Live Voltage,,, 247,,,V 
Neutral Voltage,,, 2,,,V 
Load Current,,, 0.0,,,A 
Load Test,,, 0.0,,,kVA 
Enclosure Lkg,Mains Normal,, 8,Pass,100,µA 
Enclosure Lkg,Mains Normal,SFC: Neutral Open, 12,Pass,500,µA 
Enclosure Lkg,Mains Reversed,, 8,Pass,100,µA 
Enclosure Lkg,Mains Reversed,SFC: Neutral Open, 13,Pass,500,µA 
User Comment,,,, 
Status,Pass 

Tested on,8 May 2016,,,, 
Asset ID,126555,,,, 
Rigel 288,Z48-0366,,,, 
Site,Workshop,,,, 
Location,WORKSHOP,,,, 
AP Setup,,0,,, 
User Name,Workshop,,,, 
Test Sequence,TestCode-BGC2,,,, 
Live Voltage,,, 245,,,V 
Neutral Voltage,,, 2,,,V 
Load Current,,, 0.0,,,A 
Load Test,,, 0.0,,,kVA 
Enclosure Lkg,Mains Normal,, 8,Pass,100,µA 
Enclosure Lkg,Mains Normal,SFC: Neutral Open, 12,Pass,500,µA 
Enclosure Lkg,Mains Reversed,, 8,Pass,100,µA 
Enclosure Lkg,Mains Reversed,SFC: Neutral Open, 12,Pass,500,µA 
User Comment,,,, 
Status,Pass 

我需要能夠從字符串的「測試」開始和字符串「狀態,通過」成所需要的特定資產ID如之後被命名爲單獨的文本文件的末尾單獨的每一位,「 126567.txt「 如果這可能會重複,直到文件結束,因爲將有超過3,通常在40左右。

任何幫助將非常感激。

+0

你可以發佈你*代碼「被敲我的頭幾天」 *?迄今爲止,除了文件結構之外,你什麼都沒有告訴我們。 – Lankymart

+1

你用頭部撞擊取得了什麼成就?你遇到了什麼問題?源文件是否足夠小以至於可以讀入單個字符串?這聽起來像是一個正則表達式的工作 - 你看看他們如何使用VBScript? –

回答

1

給下面的一個嘗試。我在VBA中寫過,所以如果遇到任何問題,請告訴我。我認爲使用正則表達式是分析和提取所需值的最快和最簡單的方法。如果您有任何問題,請告訴我。

Const ForReading = 1 
Dim objFSO, objFile, objRegEx 
Dim objRegRes, strMatch, strID, strLine 
Dim strFilePath, strOutFolder, strRead 

'Path to your Main File 
strFilePath = "C:\Path\ToFile\test.txt" 

'Declare the File Scripting Object To Open, Create, and Read Text Files 
Set objFSO = CreateObject("Scripting.FileSystemObject") 

'Check if File Exists 
If Not objFSO.FileExists(strFilePath) Then 
    MsgBox "Cannot Find The File" 
    WScript.Quit 
End If 

'Create Regular Expression object to parse the file 
Set objRegEx = CreateObject("VBScript.RegExp") 
objRegEx.IgnoreCase = True 
objRegEx.Global = True 
objRegEx.MultiLine = True 
'Capture all lines that starts with 'Tested' and ends with 'Status,Pass' _ 
'with a SubMatch or Capture Group for the Value between 'Asset ID,' and the next ',' 
objRegEx.Pattern = "^Tested on[\s\S]*?Asset ID\,(\S*?)\,[\s\S]*?Status\,Pass$" 

'Save the Folder Path of the Main File to a Seperate Variable 
strOutFolder = objFSO.GetParentFolderName(strFilePath) & Chr(92) 

'Open and Read the Main File into a Variable 
Set objFile = objFSO.OpenTextFile(strFilePath, ForReading, False) 
strRead = objFile.ReadAll 
objFile.Close 
Set objFile = Nothing 

'Execute the Regular Expression and Loop through the results 
Set objRegRes = objRegEx.Execute(strRead) 
For Each strMatch In objRegRes 
    strLine = Trim(strMatch) 
    strID = Trim(strMatch.SubMatches(0)) 
    'Create Individual Text Files For Each Match - *Will OverWrite Files If They Exist 
    Set objFile = objFSO.CreateTextFile(strOutFolder & strID & ".txt", True) 
    objFile.Write strLine 'Change to: 'objFile.WriteLine' if you want an ending Carriage Return 
    objFile.Close 

    'Optional Cleanup 
    Set objFile = Nothing 
    strLine = vbNullString 
    strID = vbNullString 
Next 

MsgBox "Completed" 
WScript.Quit 

我複製你的文章我測試的文本,它似乎爲我工作...