package wtf.beatrice; import java.io.File; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; public class Renamer { public static String path = "D:\\path\\to\\directory"; public static void main(String[]args) { Set listaFoto = listFilesUsingJavaIO(path); System.out.println(listaFoto.toString()); for(String currentFile : listaFoto) { System.out.println("Orig: " + currentFile); System.out.println("Nums: " + currentFile.replaceAll("[^\\d]", "")); String onlyNumbers = currentFile.replaceAll("[^\\d]", ""); String everythingElse = currentFile.replaceAll("[\\d]", ""); if(onlyNumbers.length() >= 3 || onlyNumbers.length() == 0) continue; String newName = ""; if(onlyNumbers.length() == 2) { System.out.println("size 2"); newName = "0" + onlyNumbers; } else if(onlyNumbers.length() == 1) { System.out.println("size 1"); newName = "00" + onlyNumbers; } else newName = onlyNumbers; newName = newName + everythingElse; System.out.println("New: " + newName); File original = new File(path + "\\" + currentFile); File destination = new File(path + "\\" + newName); original.renameTo(destination); } } public static Set listFilesUsingJavaIO(String dir) { return Stream.of(new File(dir).listFiles()) .filter(file -> !file.isDirectory()) .map(File::getName) .collect(Collectors.toSet()); } }