Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / macro / txt / MergeByNumberMacro.groovy @ 479

History | View | Annotate | Download (2.3 kB)

1
// STANDARD DECLARATIONS
2
package org.txm.macro.txt
3

    
4
import org.kohsuke.args4j.*
5
import groovy.transform.Field
6
import org.txm.rcpapplication.swt.widget.parameters.*
7

    
8
// BEGINNING OF PARAMETERS
9

    
10
@Field @Option(name="folder", usage="an example folder", widget="Folder", required=false, def="C:/Temp")
11
def folder
12

    
13
@Field @Option(name="ext", usage="an example string", widget="String", required=false, def=".txt")
14
def ext
15

    
16
@Field @Option(name="encoding", usage="an example string", widget="String", required=false, def="UTF-8")
17
def encoding
18

    
19

    
20
// Open the parameters input dialog box
21
if (!ParametersDialog.open(this)) return;
22

    
23
// END OF PARAMETERS
24

    
25
println "Mergin files in $folder with extension=$ext"
26

    
27
File outdir = new File(folder, "out");
28
outdir.deleteDir()
29
outdir.mkdir()
30
File errordir = new File(folder, "error");
31
errordir.deleteDir()
32
errordir.mkdir()
33

    
34
def files = folder.listFiles().findAll{File it-> it.getName().endsWith(ext)};
35
files.sort()
36
if (files.size() == 0) return;
37
def toMerge = []
38
def prefix = null
39
def prefix2;
40

    
41
for (File current : files) {
42
        prefix2 = current.getName();
43
        int idx = prefix2.indexOf(ext)
44
        if (idx <= 0) {
45
                println "error $ext file $current"
46
                new File(errordir, current.getName()) << current.getText(encoding)
47
                toMerge << current
48
                continue;
49
        }
50
        prefix2 = prefix2.substring(0, idx)
51
        int idx2 = prefix2.indexOf("-")
52
        if (idx2 <= 0) {
53
                println "error '-' file $current"
54
                new File(errordir, current.getName()) << current.getText(encoding)
55
                toMerge << current
56
                continue;
57
        }
58
        String snumber = prefix2.substring(idx2+1);
59
        prefix2 = prefix2.substring(0, idx2)
60
        
61
        if (!prefix2.equals(prefix)) {        
62
                println "merging $toMerge"
63
                File mergedFile = new File(outdir, prefix+ext)
64
                for (def f : toMerge) mergedFile << f.getText(encoding)
65
                toMerge.clear()
66
                
67
        }
68
        
69
        toMerge << current
70
        prefix = prefix2
71
}
72

    
73
if (toMerge.size() > 0) {
74
        File current = toMerge[0]
75
        prefix2 = current.getName();
76
        int idx = prefix2.indexOf(ext)
77
        prefix2 = prefix2.substring(0, idx)
78
        int idx2 = prefix2.indexOf("-")
79
        String snumber = prefix2.substring(idx2+1);
80
        prefix2 = prefix2.substring(0, idx2)
81
        
82
                println "merging $toMerge"
83
                File mergedFile = new File(outdir, prefix+ext)
84
                for (def f : toMerge) mergedFile << f.getText(encoding)
85
                toMerge.clear()
86
                
87
        println "merging last files: $toMerge"
88
        println "in $mergedFile"
89
}