VB脚本将文本分成多个文件

时间:2016-05-12 10:23:18

标签: text vbscript extract

我正在尝试创建一个VBscript文件,将文本文件分成多个文本文件。我有一段时间没有完成任何编程,而且我已经用这几天敲打了我的脑袋。

这是文本文件的一部分。

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

我需要能够从字符串的开头分离每一位"经过测试"字符串结束" Status,Pass"分成需要在特定资产ID之后命名的单独文本文件,例如" 126567.txt" 如果这可能会重复到文件结尾,因为会有超过3个,通常是40左右。

任何帮助都会非常感激。

1 个答案:

答案 0 :(得分: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

我复制了你的帖子文本以供我测试,它似乎对我有用......