import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
public class RenameFiles01 {
// 中文数字到阿拉伯数字的映射
private static final Map<String, String> chineseNumMap = createChineseNumMap();
// 创建中文数字到阿拉伯数字的映射
private static Map<String, String> createChineseNumMap() {
Map<String, String> map = new HashMap<>();
map.put("一", "1");
map.put("二", "2");
map.put("三", "3");
map.put("四", "4");
map.put("五", "5");
map.put("六", "6");
map.put("七", "7");
map.put("八", "8");
map.put("九", "9");
map.put("十", "10");
return map;
}
public static void main(String[] args) {
// 指定目录的路径 (此处写死路径,修改为你想要的路径)
String folderPath = "D:\\personal\\Desktop\\新建文件夹\\Java版本"; // 在这里修改为你的路径
File folder = new File(folderPath);
if (!folder.exists() || !folder.isDirectory()) {
System.out.println("路径无效或不是一个目录: " + folderPath);
return;
}
// 旧的后缀 (直接在程序中写死,修改为需要替换的后缀)
String oldSuffix = "_01"; // 在这里修改为你的旧后缀
// 递归遍历指定目录及子目录的文件并重命名
renameFilesInDirectory(folder, oldSuffix);
}
// 递归遍历目录并重命名文件
private static void renameFilesInDirectory(File folder, String oldSuffix) {
File[] files = folder.listFiles();
if (files == null || files.length == 0) {
System.out.println("目录 " + folder.getAbsolutePath() + " 中没有文件。");
return;
}
for (File file : files) {
if (file.isDirectory()) {
renameFilesInDirectory(file, oldSuffix); // 递归处理子目录
} else {
processFile(file, oldSuffix);
}
}
}
// 处理单个文件的重命名
private static void processFile(File file, String oldSuffix) {
String fileName = file.getName();
String parentFolderName = file.getParentFile().getName();
File grandParentFolder = file.getParentFile().getParentFile();
String grandParentFolderName = grandParentFolder != null ? grandParentFolder.getName() : "";
System.out.println("正在处理文件: " + file.getAbsolutePath());
String newSuffix = getNewSuffix(parentFolderName);
if (newSuffix == null) {
// 如果父目录没有找到匹配的中文数字,则尝试在父目录的父目录中查找
newSuffix = getNewSuffix(grandParentFolderName);
if (newSuffix == null) {
System.out.println("未找到对应的中文数字,跳过文件: " + file.getAbsolutePath());
return;
}
}
// 使用正则表达式提取旧后缀
String regex = oldSuffix + "(\\.[a-zA-Z]+)?"; // 处理可能的扩展名
String extension = "";
if (fileName.matches(".*" + regex + "$")) {
extension = fileName.replaceAll(".*" + regex, "$1");
}
String newName = createNewFileName(fileName, oldSuffix, newSuffix) + extension;
File newFile = new File(file.getParent(), newName);
// 尝试重命名文件并处理结果
if (renameFile(file, newFile)) {
System.out.println("文件 " + file.getAbsolutePath() + " 已重命名为 " + newFile.getAbsolutePath());
} else {
System.out.println("重命名文件失败: " + file.getAbsolutePath());
}
}
// 根据父目录或祖父目录的中文数字生成新的后缀
private static String getNewSuffix(String folderName) {
for (Map.Entry<String, String> entry : chineseNumMap.entrySet()) {
if (folderName.contains(entry.getKey())) {
return String.format("_%02d", Integer.parseInt(entry.getValue()));
}
}
return null; // 如果没有找到匹配的中文数字
}
// 创建新的文件名
private static String createNewFileName(String fileName, String oldSuffix, String newSuffix) {
return fileName.replaceFirst(Pattern.quote(oldSuffix) + "(?=\\.)", newSuffix);
}
// 重命名文件并返回操作是否成功
private static boolean renameFile(File oldFile, File newFile) {
try {
return oldFile.renameTo(newFile);
} catch (Exception e) {
System.out.println("文件重命名时发生错误: " + e.getMessage());
return false;
}
}
}