-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrdenaBot.java
More file actions
143 lines (123 loc) · 4.14 KB
/
OrdenaBot.java
File metadata and controls
143 lines (123 loc) · 4.14 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
import java.io.File;
public class OrdenaBot {
public static void main(String[] args) {
OrdenaBot program = new OrdenaBot();
program.start();
}
public void start() {
String home = System.getProperty("user.home");
String path = home + File.separator + "Desktop";
File desktop = new File(path);
File images = new File(path + File.separator + "Images");
File videos = new File(path + File.separator + "Videos");
File docs = new File(path + File.separator + "Docs");
images.mkdir();
videos.mkdir();
docs.mkdir();
System.out.println("Moving files...");
int countVideos = 0;
int countDocs = 0;
int countImages = 0;
if (desktop.isDirectory()) {
File[] files = desktop.listFiles();
for (int i = 0; i < files.length; i++) {
File file = files[i];
if (file.isFile()) {
if (isAnImage(file.getName())) {
if (move(file, images)) {
countImages++;
}
} else if (isAnDoc(file.getName())) {
if (move(file, docs)) {
countDocs++;
}
} else if (isAnVideo(file.getName())) {
if (move(file, videos)) {
countVideos++;
}
}
}
}
if(countImages>0){
System.out.println("Moved " + countImages + " images.");
}
if(countVideos>0){
System.out.println("Moved " + countVideos + " videos.");
}
if(countDocs>0){
System.out.println("Moved " + countDocs + " docs.");
}if((countDocs+countImages+countVideos)>0) {
System.out.println("Total: " + (countDocs + countImages + countVideos) + " files.");
}else{
System.out.println("There are not files to move.");
}
}
}
/**
* Method to move files renaming it paths
* @param source file to move
* @param target destination path
* @return new path route
*/
private boolean move(File source, File target) {
String abs = target.getAbsolutePath();
String name = source.getName();
File c = new File(abs + File.separator + name);
return source.renameTo(c);
}
/**
* Reader extension method case sensitive
* @param n String: file name
* @return boolean: true if its .png .jpg false either
*/
private boolean isAnImage(String n) {
int i = n.lastIndexOf(".");
if (i != -1) {
String sub = n.substring(i).toLowerCase();
if (sub.equals(".png")) {
return true;
}else if(sub.equals(".jpg")) {
return true;
}
}
return false;
}
/**
* Reader extension method case sensitive
* @param n String: file name
* @return boolean: true if its .pdf .txt false either
*/
private boolean isAnDoc(String n) {
int i = n.lastIndexOf(".");
if (i != -1) {
String sub = n.substring(i).toLowerCase();
if (sub.equals(".pdf")) {
return true;
}else if(sub.equals(".txt")) {
return true;
}
}
return false;
}
/**
* Reader extension method case sensitive
* @param n String: file name
* @return boolean: true if its .webm .mov .mp4 .avi false either
*/
private boolean isAnVideo(String n) {
int i = n.lastIndexOf(".");
if (i != -1) {
String sub = n.substring(i).toLowerCase();
if (sub.equals(".mov")) {
return true;
}else if(sub.equals(".webm")) {
return true;
}else if(sub.equals(".mp4")) {
return true;
}else if(sub.equals(".avi")) {
return true;
}
}
return false;
}
}