Public
Snippet $336 authored by cOOb

Rename files in java

Edited
renameFile.java
    /* Rename files with pattern "xxxx mmyy.pdf" to "xxxx yyyy-mm.pdf" 
        where year > 2000
    */

	File folder = new File("c:/myfolder");
	File[] allfiles = folder.listFiles();
	for(File f:allfiles){
	
	    int length = f.getName().length();
	    String digit = f.getName().substring(length-8, length-4);
	    System.out.print(digit);
	    String newName = "xxxxxxxx 20"+ digit.substring(2, 4) + "-" + digit.substring(0, 2)+".pdf";
	    System.out.print("  ->  " + newName);
	    // renameTo return a boolean
	    System.out.println("  ->  " + f.renameTo(new File(folder.getPath()+File.separator+newName)));
	}