import java.io.File; public class BatRename { public static long fileCount = 0; public static long dirCount = 0; public static void main(String[] args) { if (Rename("E:\\SomeDir") == false) { System.out.println("PROCESS ERROR"); } else { System.out.println("PROCESS DONE"); } System.out.println("total files : " + fileCount); System.out.println("total directories : " + dirCount); } private static boolean Rename(String path) { try { File fl = new File(path); String[] files = fl.list(); File f = null; String filename = ""; for (String file : files) { f = new File(fl, file); if (f.isDirectory()) { System.out.println("isDirectory : " + path + java.io.File.separator + file); if (Rename(path + java.io.File.separator + file) == false) { System.out.println("PROCESS ERROR"); } dirCount++; } filename = f.getName(); System.out.print(path + java.io.File.separator + filename); System.out.print("\t\t\t----->\t\t\t"); if (filename.endsWith(".temp")) { if ((new File(fl.getAbsolutePath() + java.io.File.separator + filename.replace("temp", ""))).exists() == true) { f.delete(); } else { f.renameTo(new File(fl.getAbsolutePath() + java.io.File.separator + filename.replace("temp", ""))); //使用replace 替换文件名 } } filename = f.getName(); System.out.println(path + java.io.File.separator + filename); fileCount ++; } return true; } catch (Exception e) { return false; } } }