2012-03-15 128 views
0

好日子,與特定的文件名創建從一個單一的文本文件,多個文本文件

我要尋找一些代碼來幫我不會花3周這一點。我沒有編碼知識,只是弄清楚如何使用這些代碼會很有趣。 :)如果有人可以建議一種方法來做到這一點,並寫出解決我的問題,我會非常感激。

我有一個巨大的文本文件,我想分割成多個文件。放入這些多個文件的數據以及這些文件的文件名位於源內容中。下面是永遠那張數據的樣本:

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 
... 

第一輸出文件的文件名是W1M0130.txt和內容將低於線,到下一個文件名(W1M0200)。如果可以提供幫助,文件名都以W開頭,內容行都以日期開始,除了最後一行總是$ END。

您的幫助表示讚賞。 謝謝!

+0

您的要求在大多數計算機語言中都很簡單。學習如何將源代碼轉換爲您的計算機可以執行的內容時,您會遇到困難。你能找出你的機器上有什麼可用的計算機語言,所以你不必爲計算機添加計算機語言? – 2012-03-15 15:41:34

+0

那麼,我使用Windows 7和Windows Server 2008 R2。我在2008年安裝了powershell功能,並且之前也使用了vb腳本/批處理文件。我不知道是否有可能按照我的要求去做,因爲這對我來說是最簡單的選擇:)謝謝! – user1271818 2012-03-15 15:49:57

+0

我在想Java或C++等計算機語言。無論使用哪種計算機語言,都必須安裝解釋程序或編譯器,以便Windows計算機執行源代碼。在Visual Basic腳本中執行所需的操作可能是可能的,但這不是我具備專業知識的領域。 – 2012-03-15 15:53:09

回答

1

這是我最終在VBScript中的解決方案。感謝你對那些貢獻者的幫助。

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 
0

這裏是你需要的Clojure版本。 data.txt將不得不成爲您的文件的路徑。

(def f "data.txt") 

(defn is-file [s] 
    (.startsWith s "W")) 

(defn is-end [s] 
    (= s "$END")) 

(defn file-writer [f] 
    (java.io.FileWriter. f)) 

(with-open [r (java.io.FileReader. f) 
      buffered (java.io.BufferedReader. r)] 

    (loop [l (line-seq buffered) 
     writer nil] 

    (when (seq l) 
     (let [cur-line (first l) 
      rest-lines (rest l)] 

     (cond 
     (is-file cur-line) 
     (recur rest-lines (file-writer (str cur-line ".txt"))) 

     (is-end cur-line) 
     (do 
      (.close writer) 
      (recur rest-lines nil)) 

     :else 
     (do 
      (.write writer (str cur-line "\n")) 
      (recur rest-lines writer))))))) 
+0

謝謝Joe23。我從來沒有聽說過Clojure直到現在:)我會看到我可以挖掘做出這項工作。乾杯! – user1271818 2012-03-15 16:12:48

+0

@ user1271818你提到你是在Windows 7上的。所以你可能需要Windows-Linebreaks而不是Unix的'\ r \ n'。 – Joe23 2012-03-16 08:41:56

+0

嗨。我只是不知道如何完成這項工作。我嘗試安裝ClojureBox進行編譯,但我在我頭上。任何人都有一個VB腳本解決方案? :) – user1271818 2012-03-16 17:16:44

相關問題