forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyPath.java
More file actions
143 lines (131 loc) · 3.31 KB
/
Copy pathCopyPath.java
File metadata and controls
143 lines (131 loc) · 3.31 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
package javaforce.utils;
/** CopyPath
*
* @author pquiring
*/
import java.io.*;
import java.nio.file.*;
import javaforce.*;
import javaforce.ansi.server.*;
import javaforce.jni.*;
public class CopyPath implements KeyEvents {
public static String[] args;
public ANSI ansi;
public String src;
public String dest;
public int found;
public int copied;
public int skipped;
public int total;
public int done, lastdone;
public ProgressBar progress;
public void keyPressed(int keyCode, int keyMods) {
}
public void keyTyped(char key) {
}
public static void main(String[] args) {
ANSI.enableConsoleMode();
ConsoleOutput.install();
CopyPath.args = args;
int ret = 0;
try {
new CopyPath().run();
} catch (Throwable e) {
e.printStackTrace();
ret = 1;
}
ANSI.disableConsoleMode();
System.exit(ret);
}
public void usage() {
System.out.println("CopyPath src dest");
}
public void run() {
if (args.length < 2) {
usage();
return;
}
src = args[0];
dest = args[1];
File fsrc = new File(src);
if (!fsrc.exists() || !fsrc.isDirectory()) {
System.out.println("Error:src not found");
return;
}
File fdest = new File(dest);
if (fdest.exists() && !fdest.isDirectory()) {
System.out.println("Error:dest not folder");
return;
}
if (!fdest.exists()) {
fdest.mkdirs();
}
ansi = new ANSI(this);
progress = new ProgressBar();
ansi.getConsoleSize();
int[] pos = ansi.getConsolePos();
progress.setPos(0, pos[1]);
progress.setWidth(ansi.width - 1);
lastdone = -1;
findPath(fsrc);
// System.out.println("Files found :" + found);
progress.draw();
try {
copyPath(fsrc, fdest);
if (lastdone != 100) {
progress.setValue(100);
progress.draw();
}
} catch (Exception e) {
e.printStackTrace();
}
// System.out.println("\r\n");
// System.out.println("Files copied :" + copied);
// System.out.println("Files skipped:" + skipped);
}
public void findPath(File src) {
File[] files = src.listFiles();
if (files == null) return;
for(int a=0;a<files.length;a++) {
if (files[a].isDirectory()) {
findPath(files[a]);
} else {
found++;
}
}
}
public void copyPath(File src, File dest) throws Exception {
File[] files = src.listFiles();
if (files == null) return;
for(int a=0;a<files.length;a++) {
String name = files[a].getName();
if (files[a].isDirectory()) {
File destPath = new File(dest, name);
destPath.mkdir();
copyPath(files[a], destPath);
} else {
File destFile = new File(dest, name);
if (destFile.exists()) {
long srctime = files[a].lastModified();
long desttime = destFile.lastModified();
if (desttime >= srctime) {
skipped++;
continue;
}
}
Files.copy(
Paths.get(files[a].getAbsolutePath()),
Paths.get(destFile.getAbsolutePath()),
StandardCopyOption.REPLACE_EXISTING
);
copied++;
done = (copied + skipped) * 100 / found;
if (lastdone != done) {
lastdone = done;
progress.setValue(done);
progress.draw();
}
}
}
}
}