2016-05-03 1 views
1

Erstellung habe ich diese Textdatei namens data.txtWie die Überschrift Muster enthalten, wenn mehrere Textdateien aus einer einzigen Textdatei

W1M0130 
03/12/2012 00:00 SS_001 0 0 0 0 0 0 0 0 
03/12/2012 00:00 SS_002 15 14 149 64 0 0 0 1 
03/12/2012 00:00 SS_003 4 3 233 100 0 0 0 1 
03/12/2012 00:00 SS_004 0 0 0 0 0 0 0 0 
03/12/2012 00:00 SS_005 0 0 0 0 0 0 0 0 
03/12/2012 00:00 SS_006 0 0 0 0 0 0 0 0 
03/12/2012 00:00 SS_007 0 0 0 0 0 0 0 0 
03/12/2012 00:00 SS_008 0 0 0 0 0 0 0 0 
$END 
W1M0200 
03/12/2012 00:30 SS_001 0 0 0 0 0 0 0 0 
03/12/2012 00:30 SS_002 12 11 136 58 0 0 0 1 
03/12/2012 00:30 SS_003 3 2 213 91 0 0 0 1 
03/12/2012 00:30 SS_004 0 0 0 0 0 0 0 0 
03/12/2012 00:30 SS_005 0 0 0 0 0 0 0 0 
03/12/2012 00:30 SS_006 0 0 0 0 0 0 0 0 
03/12/2012 00:30 SS_007 0 0 0 0 0 0 0 0 
03/12/2012 00:30 SS_008 0 0 0 0 0 0 0 0 
$END 
W1M0230 
... 

Und hier ist mein Code auf, wie ich diese Textdatei aufgeteilt in mehrere Textdateien:

textFile = "C:\data.txt" 
saveTo = "C:\" 
writeTo = "" 
headingPattern = "(W[0-9][A-Z][0-9]*)" 

dim fso,fileFrom,regex 
set fso = CreateObject("Scripting.FileSystemObject") 
set fileFrom = fso.OpenTextFile(textFile) 
set regex = new RegExp 

with regex 
    .Pattern = headingPattern 
    .IgnoreCase = false 
    .Global = True 
end with 

while fileFrom.AtEndOfStream <> true 
    line = fileFrom.ReadLine 
    set matches = regex.Execute(line) 

    if matches.Count > 0 then 
    writeTo = saveTo & matches(0).SubMatches(0) & ".txt" 
    set fileTo = fso.CreateTextFile(writeTo) 
    else 
    fileTo.WriteLine(line) 
    end if 
wend 

set fileFrom = nothing 
set fso = nothing 
set regex = nothing 

Einer des Ausgangs ist W1M0130.txt und dies ist die Ausgabetextdatei:

03/12/2012 00:00 SS_001 0 0 0 0 0 0 0 0 
03/12/2012 00:00 SS_002 15 14 149 64 0 0 0 1 
03/12/2012 00:00 SS_003 4 3 233 100 0 0 0 1 
03/12/2012 00:00 SS_004 0 0 0 0 0 0 0 0 
03/12/2012 00:00 SS_005 0 0 0 0 0 0 0 0 
03/12/2012 00:00 SS_006 0 0 0 0 0 0 0 0 
03/12/2012 00:00 SS_007 0 0 0 0 0 0 0 0 
03/12/2012 00:00 SS_008 0 0 0 0 0 0 0 0 
$END 

Ich es geschafft, die einzelne Textdatei in mehrere Textdateien zu teilen, aber ich habe ein Problem, wie die Überschrift Musterzeile, die (im Beispiel) W1M0130 ist.

Erwartetes Ergebnis sollte sein:

W1M0130 
03/12/2012 00:00 SS_001 0 0 0 0 0 0 0 0 
03/12/2012 00:00 SS_002 15 14 149 64 0 0 0 1 
03/12/2012 00:00 SS_003 4 3 233 100 0 0 0 1 
03/12/2012 00:00 SS_004 0 0 0 0 0 0 0 0 
03/12/2012 00:00 SS_005 0 0 0 0 0 0 0 0 
03/12/2012 00:00 SS_006 0 0 0 0 0 0 0 0 
03/12/2012 00:00 SS_007 0 0 0 0 0 0 0 0 
03/12/2012 00:00 SS_008 0 0 0 0 0 0 0 0 
$END 

ich schon im Internet nach Stunden suchen und versucht, einige Versuch und Irrtum Möglichkeiten, aber immer noch nicht das erwartete Ergebnis bekommen haben. Ihre Hilfe wird sehr geschätzt. Vielen Dank!

Antwort

3

Fügen Sie einfach fileTo.WriteLine(matches(0).SubMatches(0)) zum, wenn wie folgt aus:

if matches.Count > 0 then 
    writeTo = saveTo & matches(0).SubMatches(0) & ".txt" 
    set fileTo = fso.CreateTextFile(writeTo) 
    fileTo.WriteLine(matches(0).SubMatches(0)) 
else 
    fileTo.WriteLine(line) 
end if 

wird Ihr ausgegeben: [Notizblock W1M0130.txt]

W1M0130 
03/12/2012 00:00 SS_001 0 0 0 0 0 0 0 0 
03/12/2012 00:00 SS_002 15 14 149 64 0 0 0 1 
03/12/2012 00:00 SS_003 4 3 233 100 0 0 0 1 
03/12/2012 00:00 SS_004 0 0 0 0 0 0 0 0 
03/12/2012 00:00 SS_005 0 0 0 0 0 0 0 0 
03/12/2012 00:00 SS_006 0 0 0 0 0 0 0 0 
03/12/2012 00:00 SS_007 0 0 0 0 0 0 0 0 
03/12/2012 00:00 SS_008 0 0 0 0 0 0 0 0 
$END 
+0

Es funktioniert! Vielen Dank für die Hilfe Ani! – Djamille

+0

Gut zu wissen, dass es geholfen hat :) –

Verwandte Themen