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
|
public static String sansAccents(String source) {
return source.replaceAll("é|è|ê|ë", "e").replaceAll("É|Ê|È|Ë", "E").replaceAll("Î|Ï", "I").replaceAll("à|â|ä", "a").replaceAll("Ä|Â", "A").replaceAll("î|ï", "i").replaceAll("ô|ö", "o").replaceAll("ç", "c");
}
// Permet de traiter les noms de fichiers(supp accent, et chiffre)
public static String modifieFileName(String pathNameOld) {
StringBuilder sb = new StringBuilder();
// On enleve deja les accents sur l'ancien chemin
String pathNameNew = pathNameOld.replaceAll("é|è|ê|ë", "e").replaceAll("É|Ê|È|Ë", "E").replaceAll("Î|Ï", "I").replaceAll("à|â|ä", "a").replaceAll("Ä|Â", "A").replaceAll("î|ï", "i").replaceAll("ô|ö", "o").replaceAll("ç", "c");
// On recupere seulement le nom de fichier
String []res = pathNameNew.split("/");
String fileNameOld = res[res.length-1];
// On traite le nom du fichier
fileNameOld=fileNameOld.replaceFirst("^\\d* - /[a-zA-Z]+","");
res[res.length-1]=fileNameOld;
// On recolle les morceaux
for(int i=0; i< res.length; i++) {
sb.append(res[i]).append("/");
}
sb.setLength(sb.length()-1);
return sb.toString();
}
public static void listeRepertoire(File repertoire) {
if(repertoire.isDirectory()) {
File[] list = repertoire.listFiles();
if(list != null){
for(int i = 0; i < list.length; i++) { // Appel récursif sur les sous-répertoires
String last=list[i].toString();
if(list[i].isDirectory()) {
File newFile = new File(sansAccents(list[i].toString()));
list[i].renameTo(newFile);
}
if(list[i].isFile()) {
File newFile2 = new File(modifieFileName(list[i].toString()));
list[i].renameTo(newFile2);
}
if(last.compareTo(list[i].toString())!=0) {
System.out.println(last);
System.out.println(list[i].toString());
}
listeRepertoire(list[i]);
}
}
else {
System.err.println(repertoire + " : Erreur de lecture.");
}
}
} |
Partager