public class MultiModifyFileName {
public static void main(String[] args) {
try {
String filePath = "D:\\填写需要批量修改的文件夹根路径\\";
File file = new File(filePath);
fun(file, filePath);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 批量修改文件名称
*
* @param file 需要批量修改的文件夹对象
* @param filePath 需要批量修改的文件夹根路径
* @return void
*/
public static void fun(File file, String filePath) {
File[] files = file.listFiles();
try {
if (files.length > 0) {
for (File demo : files) {
if (demo != null) {
//如果是文件夹,则递归遍历文件下的内容
if (demo.isDirectory()) {
String fileName = demo.getName();
//这里编写文件夹名称修改逻辑
demo.renameTo(new File(filePath + fileName));
if (demo != null && demo.getPath() != null) {
fun(demo, demo.getPath() + "\\");
}
}
//如果是文件,则修改文件名称
if (demo.isFile()) {
String fileName = demo.getName();
//这里编写文件夹名称修改逻辑
demo.renameTo(new File(filePath + fileName));
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}