Bonjour,
je voudrai couper et coller seulement les 12 fichiers récents d'un répertoire vers un autre.
Mon code suivant fonctionne mais il déplace tous les fichiers présents dans le répertoire sans faire attention à la date.
Ou est mon erreur?
Merci
code:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package filesortdateinterval; import java.io.*; import java.text.*; import java.util.*; class FileFilterDateIntervalUtils implements FilenameFilter { String dateStart; String dateEnd; SimpleDateFormat sdf; public FileFilterDateIntervalUtils(String dateStart, String dateEnd) { this.dateStart = dateStart; this.dateEnd = dateEnd; sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a"); } public boolean accept(File dir, String name) { Date d = new Date(new File(dir, name).lastModified()); String current = sdf.format(d); return ((dateStart.compareTo(current) < 0 && (dateEnd.compareTo(current) >= 0))); } } class FileSortDateInterval { public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a"); FileFilterDateIntervalUtils filter = new FileFilterDateIntervalUtils("2000-01-17 11:30:00 AM", "3011-12-31 10:30:00 PM"); File folder = new File("C:/File"); File files[] = folder.listFiles(filter); for (File f : files) { System.out.println(f.getName() + " " + sdf.format(new Date(f.lastModified()))); File dir = new File("C:/Recent"); boolean success = f.renameTo(new File(dir,f.getName())); if (!success) } } }
Partager