forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeltree.java
More file actions
66 lines (62 loc) · 2.02 KB
/
Copy pathdeltree.java
File metadata and controls
66 lines (62 loc) · 2.02 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
package javaforce.utils;
/** deltree
*
* Delete files/folder that are x days old.
*
* @author peterq.admin
*/
import java.io.*;
import java.util.*;
public class deltree {
public static long timestamp;
public static int fileCount;
public static int folderCount;
public static int checkCount;
public static void main(String args[]) {
if (args.length != 2) {
System.out.println("Usage : deltree PATH days");
System.out.println(" Desc : delete all files/folder from PATH that are specifed days old or older.");
System.exit(0);
}
File path = new File(args[0]);
if (!path.isDirectory()) {
System.err.println("Error:PATH is not a folder");
System.exit(0);
}
System.out.println("Deleting files from: " + args[0]);
Calendar c = Calendar.getInstance();
timestamp = c.getTimeInMillis();
System.out.println(" now timestamp = " + c.getTimeInMillis());
timestamp -= Long.valueOf(args[1]) * 86400000L;
System.out.println("target timestamp = " + timestamp);
deleteFolder(path);
System.out.println("Deleted Files: " + fileCount);
System.out.println("Deleted Folders: " + folderCount);
}
public static void deleteFolder(File file) {
File files[] = file.listFiles();
if (files == null) return;
for(int a=0;a<files.length;a++) {
// System.out.println("checking:" + files[a].getAbsolutePath() + ":" + files[a].lastModified()); //test
checkCount++;
if (checkCount >= 1000) {
System.out.println("Files processes:" + checkCount);
checkCount = 0;
}
if (files[a].isDirectory()) {
deleteFolder(files[a]);
if (files[a].lastModified() < timestamp) {
System.out.println("Delete Folder:" + files[a].getAbsolutePath());
files[a].delete();
folderCount++;
}
} else {
if (files[a].lastModified() < timestamp) {
System.out.println("Delete File:" + files[a].getAbsolutePath());
files[a].delete();
fileCount++;
}
}
}
}
}