-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateRemover.java
More file actions
41 lines (36 loc) · 1.48 KB
/
DateRemover.java
File metadata and controls
41 lines (36 loc) · 1.48 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
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class DateRemover {
public static void main(String[] args) {
final File path = new File("C:\\Users\\Pierre\\Downloads");
final String pattern = "_";
int filesRenamed = 0;
for (File f : path.listFiles((_, name) -> name.matches("\\d{4}-\\d{2}-\\d{2}.*"))) {
String oldName = f.getName();
int stop = oldName.indexOf(pattern);
if (stop == -1) {
System.out.println("no match found in '" + oldName + "'");
continue;
}
String filetype = oldName.substring(oldName.lastIndexOf("."));
// trim to remove any trailing spaces before the filetype
String newName = oldName.substring(0, stop).trim() + filetype;
Path oldFile = f.toPath();
try {
Files.move(oldFile, oldFile.resolveSibling(newName));
System.out.println("successfully renamed '" + oldName + "' to '" +
newName + "'");
filesRenamed++;
} catch (IOException e) {
System.out.println("error when trying to rename '" + oldName + "'");
}
}
if (filesRenamed == 0) {
System.out.println("no files found and renamed");
return;
}
System.out.println(filesRenamed + " files renamed");
}
}