-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathManFiles.java
More file actions
1478 lines (1352 loc) · 49.9 KB
/
ManFiles.java
File metadata and controls
1478 lines (1352 loc) · 49.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package Jvakt;
/*
* 2024-12-12 V.61 Michael Ekdal Changed -expath to check on lowercase
* 2024-12-10 V.60 Michael Ekdal Added check of -expath also on the -ded
* 2023-12-20 V.59 Michael Ekdal Made -fsize and -tsize accept KB, MB, GB and TB in the parameter.
* 2023-09-01 V.58 Michael Ekdal Added -fsize and -tsize
* 2023-02-28 V.57 Michael Ekdal Fixed sendSTS() to not open socket twice.
* 2023-02-27 V.56 Michael Ekdal Fixat pardir to not be nulled while looping.
* 2023-02-27 V.55 Michael Ekdal Changed sendSTS() to not send every loop.
* 2022-06-23 V.54 Michael Ekdal Added getVersion() to get at consistent version throughout all classes.
*/
import java.io.*;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.*;
public class ManFiles {
static boolean swList = true, swDelete = false, swHelp = false,swParfile=false,
swSub = true, swFirst = true, swCopy = false, swRun = false, swRunJvakt = false,
swMove = false, swSettings = false, swSN = false, swDed = false, swArch = false,
swRepl = true, swAppend = false, swUnique = false, swCountC = false;
static boolean swExists = false, swFlat = false, swNew = false, swCmd = false,
swNrunq = false, swLogg = false, swNfile = false, swLoop = false, swArgs = true, swAsuf, swRsuf,swSize=false;
static boolean moved = false;
static File sdir, tdir, adir, newfile;
static String origdir, norigdir, norigdirA, suf, pos, pref, hou, min, sec, nfile, pardir,
exfile, expath, inpath, unique, infTxt, parFile, scanstr, cmd1, cmd2, newSuf, remSuf;
static String fdat = "00000000000000";
static String tdat = "99999999999999";
static Long fsize = new Long(0);
static Long tsize = new Long(0);
static Date now;
static FileFilter ff;
static Long lhou, lmin, Lsec;
static int antal, antcopies, anterrors, antdeleted, antmoved, antarchived, antded, antempty,antalCMD, p, JvLoops, sleepOrig=1;
static int antalT, antcopiesT, anterrorsT, antdeletedT, antmovedT, antarchivedT, antdedT, antemptyT, antalTCMD;
static BufferedWriter logg;
static List<String> listToS;
static FileOutputStream fis;
static OutputStreamWriter osw;
static String element;
String[] args2 = new String[3];
static String jvhost = "localhost";
static String jvport = "1956";
static String jvtype = "T";
static int port ;
static String id = null;
static String desc;
static InetAddress inet;
static String agent = null;
static String config = null;
static File configF;
static boolean swJvakt = false;
static int sleep = 1000;
static String charset = "UTF8";
static Message jmsg;
static SendMsg jm;
// static ManFiles x;
static ManFiles x = new ManFiles();
public static void main(String[] args) throws IOException,
FileNotFoundException {
suf = "*";
pref = "*";
pos = "*";
inpath = "*";
expath = "*";
exfile = "*";
hou = "0";
min = "0";
sec = "0";
scanstr = null;
charset = "UTF8";
swNrunq = false;
JvLoops = 60;
now = new Date();
System.out.println("\n*** Jvakt.ManFiles starting --- " + now);
parseParameters(args);
swArgs = false;
// if (swLogg && sLog) {
// logg.write("\n*** Starting " + now);
// logg.newLine();
// }
if (swSettings) {
infTxt = "\n swList=" + swList + "\t swDelete=" + swDelete + "\t mardir=" + pardir
+ "\t swSub=" + swSub + "\t swCopy=" + swCopy
+ "\t swMove=" + swMove + "\t swArch=" + swArch + "\t swRun=" + swRun
+ "\t sdir=" + sdir + "\t tdir=" + tdir + "\t adir=" + adir
+ "\t Suf=" + suf + "\t Prefix=" + pref + "\t swAsuf=" + swAsuf + "\t swRsuf=" + swRsuf
+ "\t Pos=" + pos + "\t Hours=" + hou
+ "\t Minutes=" + min + "\t Seconds=" + sec
+ "\t fsize=" + fsize + "\t tsize=" + tsize
+ "\t FromDate=" + fdat + "\t ToDAte=" + tdat + "\t swDed="
+ swDed + "\t swSN=" + swSN + "\t nfile=" + nfile
+ "\t exfile=" + exfile + "\t expath=" + expath
+ "\t inpath=" + inpath + "\t swRepl=" + swRepl
+ "\t swNrunq=" + swNrunq + "\t swFlat=" + swFlat
+ "\t swAppend=" + swAppend + "\t swUnique=" + swUnique + "\t swLoop=" + swLoop
+ "\t swNew=" + swNew + "\t swLogg=" + swLogg + "\t swCountC=" + swCountC +"\t scan=" + scanstr + "\t charset=" + charset+ "\t parfile=" + parFile;
System.out.println(infTxt);
if (swLogg) {
logg.write(infTxt);
logg.newLine();
}
}
if (sdir == null && !swParfile) {
System.out.println("\n>>> You must supply a source directory or a parfile <<<");
swHelp = true;
} else if (tdir != null && sdir.equals(tdir) && !swUnique ) {
System.out
.println("\n>>> You can't use the same directory as both soruce and target unless you use the -unique switch <<<");
swHelp = true;
}
if (swHelp) {
System.out
.println("\n*** Jvakt.ManFiles "+getVersion()+".61 ***"
+ "\n*** by Michael Ekdal, Sweden. ***");
System.out
.println("\nThe parameters and their meaning are:\n"
+ "\n-parfile \tThe name prefix of the parameter file (default is ManFiles). The suffix must end with .par"
+ "\n \tIn the default case, files named ManFiles01.par, ManFiles02.par and so on will be found."
+ "\n \tThe files must reside in the current directory or the directory indicated by -pardir."
+ "\n \tNOTE: -sdir and -tdir can contain maximum one consecutive space in the string."
+ "\n \tNOTE: Spaces can be substituted with a ? if more than one space are needed."
+ "\n \tNOTE: No single or double quotes are allowed anywhere in the file."
+ "\n-pardir \tThe name of the -parfile directory, like \"-pardir c:\\Temp\" (optional)"
+ "\n-sdir \tThe name of the source directory, like \"-sdir c:\\Temp\" "
+ "\n-tdir \tThe name of the target directory, like \"-tdir c:\\Temp2\" "
+ "\n-adir \tThe name of the archive directory, like \"-adir c:\\Temp3\" "
+ "\n-sub \tThe subdirectories are searched.(default) "
+ "\n-nosub \tThe subdirectories are NOT searched. "
+ "\n-copy \tCopy the files "
+ "\n-move \tMove the files "
+ "\n-del \tAll selected files are deleted from the source directory!"
+ "\n-nodel \tNo files are deleted. (default)"
+ "\n-list \tThe selected files are listed.(default) "
+ "\n-nolist \tThe selected files are NOT listed."
+ "\n-log \tWrite to specific file. like \"-log c:\\logg.txt\" "
+ "\n-run \tLive, no simulation"
+ "\n-norun \tSimulation. No copies, moves or deletions are made (default)"
+ "\n-ded \tEmpty sub-directories are deleted from the source directory."
+ "\n-noded \tNo delete of empty sub-directories. (default)"
+ "\n-repl \tReplace target files. (default)"
+ "\n-norepl \tDo NOT Replace target file."
+ "\n-nrunq \tUsed with -norepl to create a new unique file on target."
+ "\n-append \tAppend an existing target file"
+ "\n-asuf \tAdd a new suffix to the source file, like \"-asuf transfered\" "
+ "\n-rsuf \tremove the suffix of the source file, like \"-rsuf transfered\". Use \"-rsuf .\" to remove any suffix"
+ "\n-nfile \tName of the new file. Used with append to merge a number of related files."
+ "\n-unique \tThe moved or copied file is sufixed with the unique string _YYYYMMDDHHMMSS. like (_20100111113539)"
+ "\n-flat \tFiles are copied or moved to the -sdir without the original structure"
+ "\n-noflat \tFiles are copied or moved to the -sdir with the original structure (default)"
+ "\n-suf \t(include) a file suffix to look for. like \"-suf .log \" "
+ "\n \t To use more -suf strings, separate them with a semicolon. (stra;strb;strc)"
+ "\n-pref \t(include) a file prefix to look for. like \"-pref Z \" "
+ "\n \t To use more -pref strings, separate them with a semicolon like stra;strb"
+ "\n-pos \t(include) a any string in the file name to look for. like \"-pos per\" "
+ "\n \t To use more -pos strings, separate them with a semicolon like stra;strb"
+ "\n-inpath \t(include) a string in the path name. like \"-inpath INV\" "
+ "\n \t To use more -inpath strings, separate them with a semicolon like stra;strb"
+ "\n-expath \t(exclude) a string in the path name. like \"-expath INV\" "
+ "\n \t To use more -expath strings, separate them with a semicolon like stra;strb"
+ "\n-exfile \t(exclude) a string(s) in the file name. like \"-exfile TEMP01\" "
+ "\n \t To use more -exfile strings, separate them with a semicolon like flda;fldb"
+ "\n-fsize \tFrom size (-fsize 10GB gives all files bigger than 10GB) KB, MB, GB and TB accepted."
+ "\n-tsize \tTo size (-tsize 10737418240 gives all files smaller than 10GB)"
+ "\n-fdat \tSelect files from the date it was last changed . like (-fdat 20181101000000)"
+ "\n-tdat \tSelect files to the date it was last changed . like (-tdat 20181205140000)"
+ "\n-hou \tHours since file last changed. like \"-hou 48\" (default=0) "
+ "\n-min \tMinutes since file last changed. like \"-min 8\" (default=0) "
+ "\n-sec \tSeconds since file last changed. like \"-sec 30\" (default=0) "
+ "\n-nonewh \tSelect files older than the -hou value (default) "
+ "\n-newh \tSelect files newer than the -hou value"
+ "\n-sn \tShow short file name when list of files. "
+ "\n-nosn \tShow long file name when list of files. (default)"
+ "\n-? \tThis help text are shown."
+ "\n-help \tThis help text are shown."
+ "\n-set \tShows the program settings."
+ "\n-scan \tThe string that is searched for inside the files. It must be a single string with no blanks."
+ "\n-charset \tUsed with -scan. Default is UTF8. It could be UTF-16, UTF-32, ASCII, ISO8859_1..."
+ "\n-cmd1 \tThe command part1 to use on files."
+ "\n \t i.e. \"print /d:\\\\ptp165\\PBEdicom\\\" (The selected filename is used as a parameter to the command)"
+ "\n-cmd2 \tThe command part2 to use on files."
+ "\n-countc \tShows the number of children in each traversed directory."
+ "\n-jvakt \tA switch to enable a report OK or ERR to Jvakt. Default is no reporting to Jvakt. The file Jvakt.properties must be provided"
+ "\n-id \tUsed as identifier in the Jvakt monitoring system. Mandatory if -jvakt switch is used."
+ "\n-config \tThe directory where to find the Jvakt.properties file. like \"-config c:\\Temp\". Optional. Default is the current directory."
+ "\n-loop \tExecutes every second in a never ending loop. -noloop is the default value."
+ "\n-sleep \tIn seconds. Used with loop to sleep between checks. The default is one second."
+ "\n\nComments:"
+ "\nErrorlevel is set to the number of found files. Max value is 255 though.\n");
System.exit(12);
}
for (;;) {
now = new Date();
if (swParfile) {
// System.out.println("--> innan readParFile");
readParFile(); // reads the parameter files.
for(Object object : listToS) {
// String element = (String) object;
element = (String) object;
System.out.println("\n*** ParRow * "+ new Date()+" * " + element);
String[] tab = null;
tab = element.split("\\s+"); // split on one or many white spaces
parseParameters(tab);
execOneParSet(); // execute one line set of parameters from the parfile.
}
}
else {
element = Arrays.toString(args);
System.out.println("\n*** ParRow * "+ new Date()+" * " + element);
execOneParSet(); // execute the set of parameters from the command line.
}
System.out.println("\n*** Total - Files found:" + antalT + " deleted:" + antdeletedT
+ " copied:" + antcopiesT + " moved:" + antmovedT + " archived:" + antarchivedT + " errors:"
+ anterrorsT + " empty:" + antemptyT + " del dir:" + antdedT+ " cmd:"+antalTCMD);
// if (antal>0) sLog=true; // Hittades filer vill vi ha avslutande logg
if (swLogg && antalT>0 && !swParfile) {
// logg.newLine();
logg.write("*** Total - Files found:" + antalT + " deleted:" + antdeletedT
+ " copied:" + antcopiesT + " moved:" + antmovedT + " archived:" + antarchivedT
+ " errors:" + anterrorsT + " empty:" + antemptyT
+ " del dir:" + antdedT+ " cmd:"+antalTCMD);
logg.newLine();
}
if (swRunJvakt && swJvakt) {
if (anterrorsT == 0) {
if (JvLoops>=60) { // counts "loops" to make the OK lag at least 60 seconds.
desc = "No error found in ManFiles";
sendSTS(true);
JvLoops=0;
}
// else JvLoops=JvLoops+sleepOrig;
else JvLoops++;
}
else {
if (JvLoops>=60) {
desc = "Errors found in ManFiles! Check log files.";
sendSTS(false);
JvLoops=0;
}
else JvLoops++;
}
}
// if not a loop, break out of the loop and finish the program.
if (!swLoop) break;
antalT=0;antdeletedT=0;antcopiesT=0;antmovedT=0;antarchivedT=0;anterrorsT=0;antemptyT=0;antdedT=0;antalTCMD=0;
// sleep for one second and then do it all over again.
try {
// Thread.currentThread().sleep(1000);
Thread.sleep(sleep);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
now = new Date();
System.out.println("\n*** Finished " + now);
if (swLogg && antalT>0 && !swParfile) {
// logg.write("*** Finished " + now);
// logg.newLine();
logg.close();
}
// if (antal>0) System.exit(0);
// else System.exit(4);
if (antalT > 255) antalT = 255;
System.exit(antalT);
}
static private String getVersion() {
String version = "0";
try {
Class<?> c1 = Class.forName("Jvakt.Version",false,ClassLoader.getSystemClassLoader());
Version ver = new Version();
version = ver.getVersion();
}
catch (java.lang.ClassNotFoundException ex) {
version = "?";
}
return version;
}
static void execOneParSet() throws IOException, FileNotFoundException {
// System.out.println("\n---> Execute Parameter set " + );
antal=0; antcopies=0; anterrors=0; antdeleted=0; antmoved=0; antarchived=0; antded=0; antempty=0; antalCMD=0; p=0;
swFirst = true;
String dat = new String("yyyyMMdd");
String tim = new String("HHmmss");
SimpleDateFormat dat_form;
if (swUnique || swNrunq || swArch) {
dat_form = new SimpleDateFormat(dat);
unique = dat_form.format(now);
dat_form = new SimpleDateFormat(tim);
unique = "_" + unique + dat_form.format(now);
}
lhou = new Long(hou);
lmin = new Long(min);
Lsec = new Long(sec);
// x = new ManFiles();
// System.out.println("pref: "+pref+" inpath: "+inpath+" sdir "+ sdir+" fsize "+fsize+" tsize"+tsize);
ff = x.new FileFilter(lhou, lmin, Lsec, suf, pos, pref, expath, inpath, swNew, exfile,scanstr,charset,fdat,tdat,fsize,tsize);
x.new VisitAllFiles(sdir);
if (swParfile) {
System.out.println("*** ParRow - Files found:" + antal + " deleted:" + antdeleted
+ " copied:" + antcopies + " moved:" + antmoved + " archived:" + antarchived + " errors:"
+ anterrors + " empty:" + antempty + " del dir:" + antded+ " cmd:"+antalCMD);
// if (antal>0) sLog=true; // Hittades filer vill vi ha avslutande logg
if (swLogg && antal>0) {
logg.write("*** ParRow - Files found:" + antal + " deleted:" + antdeleted
+ " copied:" + antcopies + " moved:" + antmoved + " archived:" + antarchived
+ " errors:" + anterrors + " empty:" + antempty
+ " del dir:" + antded+ " cmd:"+antalCMD);
logg.newLine();
logg.flush();
}
}
}
static void parseParameters(String[] args ) throws IOException, FileNotFoundException {
// System.out.println("---> parse " +args[0]);
String ssdir = null;
swList = true; swDelete = false; swHelp = false; swAsuf = false; swRsuf = false;
swSub = true; swFirst = true; swCopy = false; swRun = false;
swMove = false; swArch = false; swSettings = false; swSN = false; swDed = false;
swRepl = true; swAppend = false; swUnique = false; swCountC = false;
swExists = false; swFlat = false; swNew = false; swCmd = false;
swNrunq = false; swLogg = false; swNfile = false;
moved = false;
sdir=null; tdir=null; newfile=null;
origdir=null; norigdir=null; norigdirA=null; nfile=null;
unique=null; infTxt=null;
suf = "*";
newSuf = null;
remSuf = null;
pref = "*";
pos = "*";
inpath = "*";
expath = "*";
exfile = "*";
hou = "0";
min = "0";
sec = "0";
scanstr = null;
charset = "UTF8";
cmd1 = "";
cmd2 = "";
fdat = "00000000000000";
tdat = "99999999999999";
fsize = new Long(0);
tsize = new Long(0);
for (int i = 0; i < args.length; i++) {
// System.out.println("---> parse " + i +" " +args[i]);
if (args[i].equalsIgnoreCase("-list"))
swList = true;
else if (args[i].equalsIgnoreCase("-nolist"))
swList = false;
else if (args[i].equalsIgnoreCase("-copy"))
swCopy = true;
else if (args[i].equalsIgnoreCase("-move"))
swMove = true;
else if (args[i].equalsIgnoreCase("-del"))
swDelete = true;
else if (args[i].equalsIgnoreCase("-asuf")){
newSuf = args[++i];
swAsuf = true;
}
else if (args[i].equalsIgnoreCase("-rsuf")){
remSuf = args[++i];
swRsuf = true;
}
else if (args[i].equalsIgnoreCase("-nodel"))
swDelete = false;
else if (args[i].equalsIgnoreCase("-sub"))
swSub = true;
else if (args[i].equalsIgnoreCase("-nosub"))
swSub = false;
else if (args[i].equalsIgnoreCase("-run"))
swRun = true;
else if (args[i].equalsIgnoreCase("-norun"))
swRun = false;
else if (args[i].equalsIgnoreCase("-help"))
swHelp = true;
else if (args[i].equalsIgnoreCase("-?"))
swHelp = true;
else if (args[i].equalsIgnoreCase("-set"))
swSettings = true;
else if (args[i].equalsIgnoreCase("-ded"))
swDed = true;
else if (args[i].equalsIgnoreCase("-noded"))
swDed = false;
else if (args[i].equalsIgnoreCase("-loop"))
swLoop = true;
else if (args[i].equalsIgnoreCase("-sn"))
swSN = true;
else if (args[i].equalsIgnoreCase("-nosn"))
swSN = false;
else if (args[i].equalsIgnoreCase("-repl"))
swRepl = true;
else if (args[i].equalsIgnoreCase("-norepl"))
swRepl = false;
else if (args[i].equalsIgnoreCase("-nrunq"))
swNrunq = true;
else if (args[i].equalsIgnoreCase("-sdir")) {
ssdir = args[++i];
while( args.length > i+1 ) {
if ( args[i+1].length() > 2 && args[i+1].startsWith("-")) break;
ssdir = ssdir+" "+args[++i];
}
// Character in need of escapes <([{\^-=$!|]})?*+.>
ssdir = ssdir.replaceAll("[\\*\\?\\<\\>\\|]" , " ");
// System.out.println("ssdir: "+ssdir);
sdir = new File(ssdir);
origdir = sdir.toString();
} else if (args[i].equalsIgnoreCase("-tdir")) {
ssdir = args[++i];
while( args.length > i+1 ) {
if ( args[i+1].length() > 2 && args[i+1].startsWith("-")) break;
ssdir = ssdir+" "+args[++i];
}
ssdir = ssdir.replaceAll("[\\*\\?\\<\\>\\|]" , " ");
tdir = new File(ssdir);
norigdir = tdir.toString();
} else if (args[i].equalsIgnoreCase("-adir")) {
swArch = true;
ssdir = args[++i];
while( args.length > i+1 ) {
if ( args[i+1].length() > 2 && args[i+1].startsWith("-")) break;
ssdir = ssdir+" "+args[++i];
}
ssdir = ssdir.replaceAll("[\\*\\?\\<\\>\\|]" , " ");
adir = new File(ssdir);
norigdirA = adir.toString();
} else if (args[i].equalsIgnoreCase("-suf"))
suf = args[++i].trim();
else if (args[i].equalsIgnoreCase("-pos"))
pos = args[++i].trim();
else if (args[i].equalsIgnoreCase("-pref"))
pref = args[++i].trim();
else if (args[i].equalsIgnoreCase("-hou"))
hou = args[++i];
else if (args[i].equalsIgnoreCase("-min"))
min = args[++i];
else if (args[i].equalsIgnoreCase("-sec"))
sec = args[++i];
else if (args[i].equalsIgnoreCase("-newh"))
swNew = true;
else if (args[i].equalsIgnoreCase("-nonewh"))
swNew = false;
else if (args[i].equalsIgnoreCase("-nfile")) {
nfile = args[++i];
swNfile = true;
} else if (args[i].equalsIgnoreCase("-exfile"))
exfile = args[++i];
else if (args[i].equalsIgnoreCase("-expath"))
expath = args[++i];
else if (args[i].equalsIgnoreCase("-inpath"))
inpath = args[++i];
else if (args[i].equalsIgnoreCase("-flat"))
swFlat = true;
else if (args[i].equalsIgnoreCase("-append"))
swAppend = true;
else if (args[i].equalsIgnoreCase("-unique"))
swUnique = true;
else if (args[i].equalsIgnoreCase("-log")) {
swLogg = true;
fis = new FileOutputStream(args[++i], true);
osw = new OutputStreamWriter(fis, "Cp850");
logg = new BufferedWriter(osw);
}
else if (args[i].equalsIgnoreCase("-countc"))
swCountC = true;
else if (args[i].equalsIgnoreCase("-scan"))
scanstr = args[++i];
else if (args[i].equalsIgnoreCase("-charset")) charset = args[++i];
else if (args[i].equalsIgnoreCase("-cmd1")) {
cmd1 = args[++i]; swCmd = true;
while( args.length > i+1 ) {
if ( args[i+1].length() > 2 && args[i+1].startsWith("-")) break;
cmd1 = cmd1+" "+args[++i];
}
}
else if (args[i].equalsIgnoreCase("-cmd2")) {
cmd2 = args[++i]; swCmd = true;
while( args.length > i+1 ) {
if ( args[i+1].length() > 2 && args[i+1].startsWith("-")) break;
cmd2 = cmd2+" "+args[++i];
}
}
else if (args[i].equalsIgnoreCase("-fdat"))
fdat = args[++i];
else if (args[i].equalsIgnoreCase("-tdat"))
tdat = args[++i];
else if (args[i].equalsIgnoreCase("-pardir"))
pardir = args[++i];
else if (args[i].equalsIgnoreCase("-parfile")) {
swParfile = true;
if (args.length == i+1) parFile = "ManFiles";
else parFile = args[++i];
if (parFile.startsWith("-")) {
parFile = "ManFiles";
i--;
}
// System.out.println("---> parFile: " + parFile);
}
else if (args[i].equalsIgnoreCase("-jvakt")) swJvakt=true;
else if (args[i].equalsIgnoreCase("-config")) config = args[++i];
else if (args[i].equalsIgnoreCase("-id")) id = args[++i];
else if (args[i].equalsIgnoreCase("-sleep")) {
sleep = Integer.valueOf(args[++i]);
sleepOrig = sleep;
sleep = sleep *1000;
}
else if (args[i].equalsIgnoreCase("-fsize")) {
i++;
if (args[i].toLowerCase().endsWith("kb")) {
fsize = Long.valueOf(args[i].substring(0, args[i].length()-2));
fsize = fsize*1024; // 1024*1024 to get mb
}
else if (args[i].toLowerCase().endsWith("mb")) {
fsize = Long.valueOf(args[i].substring(0, args[i].length()-2));
fsize = fsize*1048576; // 1024*1024 to get mb
}
else if (args[i].toLowerCase().endsWith("gb")) {
fsize = Long.valueOf(args[i].substring(0, args[i].length()-2));
fsize = fsize*1073741824; // 1024*1024*1024 to get gb
}
else if (args[i].toLowerCase().endsWith("tb")) {
fsize = Long.valueOf(args[i].substring(0, args[i].length()-2));
fsize = fsize*1073741824*1024; // 1024*1024*1024*1024 to get tb
}
else {
fsize = Long.valueOf(args[i]);
}
swSize=true;
// System.out.println("---> fsize = "+fsize );
}
else if (args[i].equalsIgnoreCase("-tsize")) {
i++;
if (args[i].toLowerCase().endsWith("kb")) {
tsize = Long.valueOf(args[i].substring(0, args[i].length()-2));
tsize = tsize*1024; // 1024*1024 to get mb
}
else if (args[i].toLowerCase().endsWith("mb")) {
tsize = Long.valueOf(args[i].substring(0, args[i].length()-2));
tsize = tsize*1048576; // 1024*1024 to get mb
}
else if (args[i].toLowerCase().endsWith("gb")) {
tsize = Long.valueOf(args[i].substring(0, args[i].length()-2));
tsize = tsize*1073741824; // 1024*1024*1024 to get gb
}
else if (args[i].toLowerCase().endsWith("tb")) {
tsize = Long.valueOf(args[i].substring(0, args[i].length()-2));
tsize = tsize*1073741824*1024; // 1024*1024*1024*1024 to get tb
}
else {
tsize = Long.valueOf(args[i]);
}
swSize=true;
// System.out.println("---> tsize = "+tsize );
}
}
if (tdir == null) {
swCopy = false;
swMove = false;
} // Use copy if both is present.
if (swCopy && swMove) {
swCopy = true;
swMove = false;
} // Use move instead of copy and delete.
if (swCopy && swDelete && !swAppend) {
swCopy = false;
swMove = true;
} // If move is true you cannot change the source file name.
if (swMove || swDelete) {
swAsuf = false;
swRsuf = false;
} // Use move instead of copy and delete.
if (swAsuf && swRsuf) {
System.out.println("*** Both -asuf and -rsuf are present. Only -asuf is used!");
swRsuf = false;
}
if (sdir == null && !swArgs) {
System.out.println("**** -sdir is missing!! Execution aborted!!!");
System.exit(12);
}
// if (sdir == null && !swParfile) {
// System.out.println("**** Both -sdir and -parfile is missing!!");
// System.exit(12);
// }
if (swJvakt && id != null && swArgs ) {
swRunJvakt = swRun;
if (swParfile) swRunJvakt=true;
if (config == null ) configF = new File("Jvakt.properties");
else configF = new File(config,"Jvakt.properties");
System.out.println("-config file: "+configF);
getProps();
}
}
static void readParFile() {
System.out.println("-- readParFile 1 ");
File[] listf;
DirFilter df;
String s;
String suf = ".par";
String pos = parFile;
int antal = 0;
File dir = new File(".");
if (pardir != null ) dir = new File(pardir);
System.out.println("-- pardir "+pardir+" dir "+dir);
listToS = new ArrayList<String>(); // id:mailadress.
df = new DirFilter(suf, pos);
listf = dir.listFiles(df);
System.out.println("\n*** Number of parameter files found: "+ listf.length);
try {
BufferedReader in;
for (int i = 0; i < listf.length; i++) {
System.out.println("-- Importing parfile: "+listf[i]);
in = new BufferedReader(new FileReader(listf[i]));
while ((s = in.readLine()) != null) {
if (s.length() == 0) continue;
if (s.startsWith("#")) continue;
listToS.add(s);
antal++;
// System.out.println("-- add: "+s);
}
in.close();
}
} catch (Exception e) { System.out.println(e); }
System.out.println("-- Number of rows imported: "+ antal);
}
// Copies src file to dst file.
// If the dst file does not exist, it is created.
// if swAppend the file is appended it it exist.
protected void copy(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
OutputStream out;
if (swAppend)
out = new FileOutputStream(dst, true);
else
out = new FileOutputStream(dst);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
// sends status to the Jvakt server
// static protected void sendSTS( boolean STS) throws IOException {
static protected void sendSTS( boolean STS) {
System.out.println("\n--- " + id + " -- " + desc);
System.out.println("--- Connecting to "+jvhost+":"+jvport);
jmsg = new Message();
jm = new SendMsg(jvhost, port);
try {
String reply = jm.open();
System.out.println("Status: "+reply);
if (!reply.startsWith("failed")) {
if (STS) jmsg.setRptsts("OK");
else jmsg.setRptsts("ERR");
jmsg.setId(id);
jmsg.setType(jvtype);
jmsg.setId(id);
jmsg.setType("T");
jmsg.setBody(desc);
jmsg.setAgent(agent);
if (jm.sendMsg(jmsg)) System.out.println("--- Rpt Delivered -- " + id + " -- " + desc);
else System.out.println("--- Rpt Failed ---");
jm.close();
}
else {
if (reply.equalsIgnoreCase("failed")) System.out.println("-- Rpt Failed --");
else System.out.println("-- Test to Jvakt server succeeded - "+reply);
}
}
// catch (java.net.ConnectException e ) {System.out.println("-- Rpt Failed --" + e); }
catch (NullPointerException npe2 ) {System.out.println("-- Rpt Failed --" + npe2);}
}
static void getProps() {
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream(configF);
prop.load(input);
// get the property value and print it out
jvport = prop.getProperty("jvport");
jvhost = prop.getProperty("jvhost");
port = Integer.parseInt(jvport);
System.out.println("getProps jvport: " + jvport + " jvhost: "+jvhost) ;
} catch (IOException ex) {
ex.printStackTrace();
}
try {
inet = InetAddress.getLocalHost();
System.out.println("-- Inet: "+inet);
agent = inet.toString();
}
catch (Exception e) { System.out.println(e); }
}
// ** start internal classes **
// Process only files under sdir
protected class VisitAllFiles {
VisitAllFiles(File sdir) throws IOException {
// System.out.println("---> class visitAllFiles " + dir);
boolean copyerror;
boolean moveerror;
boolean archiveerror;
String newDir2 = null;
String newDirA = null;
if (sdir.isDirectory() && swFirst) {
swFirst = swSub;
String[] children = sdir.list(ff);
if (children == null ) {
// System.out.println("---> class visitAllFiles " + dir);
return;
}
if (swCountC) System.out.println("# Childrens; "+children.length+" ; "+sdir);
for (int i = 0; i < children.length; i++) {
new VisitAllFiles(new File(sdir, children[i]));
}
if (children.length == 0) {
if (swDed) {
boolean swFoundexPath = false;
String expathTab[] = expath.split(";");
for (int k = 0; k < expathTab.length; k++) {
if (sdir.toString().toLowerCase().indexOf(expathTab[k].toLowerCase()) >= 0) {
swFoundexPath = true;
if (swList) {
System.out.println("Empty directory '"+sdir+"' deletion excluded because expath.");
if (swLogg) {
logg.write("Empty directory '"+sdir+"' deletion excluded because expath.");
logg.newLine();
}
}
}
}
if (!swFoundexPath) {
antempty++; antemptyT++;
if (swList) {
System.out.println("Empty directory to be deleted: " + sdir);
if (swLogg) {
logg.write("Empty directory to be deleted: " + sdir);
logg.newLine();
}
}
if (swRun) {
if (sdir.delete()) {
antded++; antdedT++;
}
else {
anterrors++; anterrorsT++;
if (swList) {
System.out.println("Deletion failed: " + sdir);
if (swLogg) {
logg.write("Deletion failed: " + sdir);
logg.newLine();
}
}
}
}
}
}
}
} else {
if (sdir.isFile()) {
if (swLogg && antal == 0 && element != null) {
logg.newLine();
// logg.write("*** Starting " + new Date());
// logg.newLine();
// if (element != null) {
logg.write("*** ParRow * "+ new Date()+" * "+element);
logg.newLine();
// }
}
antal++; antalT++;
copyerror = false;
moveerror = false;
archiveerror = false;
swExists = false;
moved = false;
if (swCmd) {
args2[0] = cmd1;
args2[1] = sdir.getAbsolutePath();
args2[2] = cmd2;
if (swList) System.out.println("-cmd: "+args2[0]+" \""+args2[1]+"\" "+args2[2]);
if (swRun) {
runCMD pp = new runCMD(args2);
if (pp.runCMDfile()) {
antalCMD++; antalTCMD++;
if (swList) {
System.out.println(" -successfull cmd: "+args2[0]+" \""+args2[1]+"\" "+args2[2]);
if (swLogg) {
logg.write(" -successfull cmd: "+args2[0]+" \""+args2[1]+"\" "+args2[2]);
logg.newLine();
}
}
}
else {
swRun = false; // do not proceed if cmd failed
anterrors++;
if (swList) {
System.out.println(" -failed cmd: "+args2[0]+" \""+args2[1]+"\" "+args2[2]);
if (swLogg) {
logg.write(" -failed cmd: "+args2[0]+" \""+args2[1]+"\" "+args2[2]);
logg.newLine();
}
}
}
}
}
if (swList) {
Long tSize;
String tUnit="";
tSize = sdir.length();
if (tSize>1048576) { tSize = tSize/1024; tUnit="KB"; }
if (tSize>1048576) { tSize = tSize/1024; tUnit="MB"; }
if (tSize>1048576) { tSize = tSize/1024; tUnit="GB"; }
if (tSize>1048576) { tSize = tSize/1024; tUnit="TB"; }
if (swSN) {
if (swMove || swCopy || swDelete) System.out.print("-File: "+sdir.getName());
else System.out.println("-File: "+sdir.getName()+" Size: "+tSize+tUnit);
if (swLogg) {
if (swMove || swCopy || swDelete)
logg.write("-File: "+sdir.getName());
else {
logg.write("-File: "+sdir.getName()+" Size: "+tSize+tUnit);
logg.newLine();
}
}
} else {
if (swMove || swCopy || swDelete) System.out.print("-File: "+sdir);
else System.out.println("-File: "+sdir+" Size: "+tSize+tUnit);
if (swLogg) {
if (swMove || swCopy || swDelete)
logg.write("-File: "+sdir.getAbsolutePath());
else {
// System.out.println("-FileXX: "+sdir.getAbsolutePath());
logg.write("-File: "+sdir.getAbsolutePath()+" Size: "+tSize+tUnit);
logg.newLine();
}
}
}
}
if (swMove || swCopy) {
if (!swFlat) {
newDir2 = norigdir+sdir.getPath().substring(origdir.length(),(sdir.getPath().length()-sdir.getName().length()-1));
tdir = new File(newDir2);
} else {
newDir2 = norigdir;
tdir = new File(newDir2);
}
if (!tdir.exists() && swRun)
tdir.mkdirs();
}
if (swArch) {
if (!swFlat) {
newDirA = norigdirA+sdir.getPath().substring(origdir.length(),(sdir.getPath().length()-sdir.getName().length()-1));
adir = new File(newDirA);
} else {
newDirA = norigdirA;
adir = new File(newDirA);
}
// newDirA = norigdirA+sdir.getPath().substring(origdir.length(),(sdir.getPath().length()-sdir.getName().length()-1));
// adir = new File(newDirA);
newfile = new File(newDirA, sdir.getName());
if (!adir.exists() && swRun)
adir.mkdirs();
if (newfile.exists()) {
p = sdir.getName().lastIndexOf(".");
if (p >= 0)
newfile = new File(newDirA, sdir.getName().substring(0, p)+ unique+ sdir.getName().substring(p));
else
newfile = new File(newDirA, sdir.getName()+ unique);
}
if (swList) {
// System.out.print(" -archived> " + newfile);
if (!swMove && !swCopy) System.out.println(" -archived> " + newfile);
else System.out.print(" -archived> " + newfile);
if (swLogg) {
logg.write(" -archived> " + newfile);
if (!swMove && !swCopy) logg.newLine();
}
}
try {
if (swRun ) {
copy(sdir, newfile);
antarchived++; antarchivedT++;
}
} catch (IOException e) {
swRun = false; // do not proceed if arcive failed
archiveerror = true;
anterrors++;
System.out.println(e);
if (swList)
System.out.println(" *** archive error --> " + newfile);
}
}
if (swMove) {
// newfile = new File(newDir2,dir.getName());
if (swUnique) {
p = sdir.getName().lastIndexOf(".");
if (p >= 0) newfile = new File(newDir2, sdir.getName().substring(0, p)+ unique+ sdir.getName().substring(p));
else newfile = new File(newDir2, sdir.getName()+ unique);
} else {
if (swNfile) newfile = new File(newDir2, nfile);
else newfile = new File(newDir2, sdir.getName());
}
if (!swRepl) { // If no-replace, do a check if target
// exists.
if (newfile.exists())
swExists = true;
if (swExists && swNrunq) {
swExists = false;
p = sdir.getName().lastIndexOf(".");
if (p >= 0)
newfile = new File(newDir2, sdir.getName().substring(0, p)+ unique+ sdir.getName().substring(p));
else
newfile = new File(newDir2, sdir.getName()+ unique);
}
}
if (swList && (swRepl || !swExists)) {
System.out.println(" -moved> " + newfile);
if (swLogg) {