2012-03-15 191 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

相关问题