2016-05-12 4 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

頭を叩きながら何を達成しましたか?どのような問題が発生しましたか?ソースファイルは、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 

あなたは*「数日間で私の頭を叩いされて」きたコードを投稿することができ

関連する問題