java文件批量重命名小工具
文件批量重命名
- 第一种类型:将某文件夹下所有文件和目录中含有的某些字符串替换为另外对应的某些字符串
启动后直接访问地址 ip:8080/api/fileRename/rename
- 第二种类型:将某文件夹下所有文件和目录中含有的某些字符串替换为另外对应的某些字符串
启动后直接访问地址 ip:8080/api/fileRename/renameFileByFileName
使用方法
在application.yml中配置
-
ini文件位置:iniFilePath 指向具体文件 -
需要重命名文件夹位置:targetFolderPath 指向文件夹 -
日志存放位置:logPath 指向具体文件 如没有文件会自动创建,但是文件夹需要存在
直接启动浏览器访问地址即可,后台存在输出可以查看重命名情况
config文件说明
config.ini 文件为第二种类型的配置文件
-
oldFileName:旧文件名 与新文件名对应位置的文件名一一对应
-
newFileName:新文件名 与旧文件名对应位置的文件名一一对应
-
oldDirectoryFileName:旧文件夹名, 与新文件夹名对应位置的文件名一一对应
-
newDirectoryFileName:新文件夹名, 与旧文件夹名对应位置的文件名一一对应
config1文件说明
config1.ini 文件为第一种类型的配置文件
-
oldName:原文件名需要替换的字符串,与newName一一对应
-
newName:新文件名需要换进的字符串,与oldName一一对应
package com.lwl.filerename.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 文件重命名
*/
@RestController
@RequestMapping("/fileRename")
public class fileRenameController {
@Value("${lwl.iniFilePath}")
private String iniFilePath;
@Value("${lwl.targetFolderPath}")
private String targetFolderPath;
@Value("${lwl.logPath}")
private String logPath;
/**
* 替换文件名中部分内容
* 此处使用config1.ini
*
* @param args
*/
public static void main_part(String[] args) {
// 指定INI文件路径和目标文件夹路径
String iniFilePath = "F:\file-rename\src\main\resources\config1.ini";
String targetFolderPath = "F:\test-txt";
String logPath = "F:\log.txt";
// 文件名修改数量
AtomicInteger fileRenameCount = new AtomicInteger();
// 文件夹名修改数量
AtomicInteger floderRenameCount = new AtomicInteger();
// 文件名修改失败数量
AtomicInteger fileRenameCountFail = new AtomicInteger();
// 文件夹名修改失败数量
AtomicInteger floderRenameCountFail = new AtomicInteger();
try {
// 读取INI文件内容
Properties properties = new Properties();
properties.load(new FileInputStream(iniFilePath));
// 获取旧文件名和新文件名列表
String oldNamesStr = properties.getProperty("oldName");
String newNamesStr = properties.getProperty("newName");
List<String> oldNames = Arrays.asList(oldNamesStr.split(","));
List<String> newNames = Arrays.asList(newNamesStr.split(","));
// 确保两个列表长度相同
if (oldNames.size() != newNames.size()) {
throw new IllegalArgumentException("Old names and new names lists must have the same length.");
}
// 遍历目标文件夹及其子文件夹中的所有文件并重命名
Files.walk(Paths.get(targetFolderPath))
.filter(Files::isRegularFile)
.forEach(oldFilePath -> {
String oldName = oldFilePath.getFileName().toString().trim();
String newName = oldName;
for (int i = 0; i < oldNames.size(); i++) {
newName = newName.replace(oldNames.get(i).trim(), newNames.get(i).trim());
}
if (!newName.equals(oldName)) {
Path newFilePath = oldFilePath.resolveSibling(newName);
try {
Files.move(oldFilePath, newFilePath, StandardCopyOption.REPLACE_EXISTING);
System.out.println("Renamed file: " + oldName + " -> " + newName);
logMessage("Renamed file: " + oldName + " -> " + newName, logPath);
fileRenameCount.getAndIncrement();
} catch (IOException e) {
System.err.println("Failed to rename file: " + oldName);
logMessage("Failed to rename file: " + oldName, logPath);
e.printStackTrace();
fileRenameCountFail.getAndIncrement();
}
} else {
System.out.println("No rename rule found for file: " + oldName);
logMessage("No rename rule found for file: " + oldName, logPath);
}
});
// 遍历目标文件夹及其子文件夹中的所有文件夹并重命名
Files.walk(Paths.get(targetFolderPath))
.filter(Files::isDirectory)
.sorted(Comparator.comparingInt((Path p) -> p.getNameCount()).reversed()) // 从最深的子目录开始重命名
.forEach(oldDirPath -> {
String oldName = oldDirPath.getFileName().toString().trim();
String newName = oldName;
for (int i = 0; i < oldNames.size(); i++) {
newName = newName.replace(oldNames.get(i).trim(), newNames.get(i).trim());
}
if (!newName.equals(oldName)) {
Path newDirPath = oldDirPath.resolveSibling(newName);
try {
Files.move(oldDirPath, newDirPath, StandardCopyOption.REPLACE_EXISTING);
System.out.println("Renamed directory: " + oldName + " -> " + newName);
logMessage("Renamed directory: " + oldName + " -> " + newName, logPath);
floderRenameCount.getAndIncrement();
} catch (IOException e) {
System.err.println("Failed to rename directory: " + oldName);
logMessage("Failed to rename directory: " + oldName, logPath);
e.printStackTrace();
floderRenameCountFail.getAndIncrement();
}
} else {
System.out.println("No rename rule found for directory: " + oldName);
logMessage("No rename rule found for directory: " + oldName, logPath);
}
});
System.out.println("file rename count:" + fileRenameCount.get());
logMessage("file rename count:" + fileRenameCount.get(), logPath);
System.out.println("directory rename count:" + floderRenameCount.get());
logMessage("directory rename count:" + floderRenameCount.get(), logPath);
System.out.println("file rename failed count:" + fileRenameCountFail.get());
logMessage("file rename failed count:" + fileRenameCountFail.get(), logPath);
System.out.println("directory rename failed count:" + floderRenameCountFail.get());
logMessage("directory rename failed count:" + floderRenameCountFail.get(), logPath);
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
System.err.println(e.getMessage());
}
}
/**
* 根据全路径替换全部文件名
* 此处使用config.ini
*
* @param args
*/
public static void main(String[] args) {
// 指定INI文件路径和目标文件夹路径
String iniFilePath = "F:\程序\code\file-rename\src\main\resources\config.ini";
String targetFolderPath = "F:\程序\重命名";
String logPath = "F:\程序\log.txt";
// 文件名修改数量
AtomicInteger fileRenameCount = new AtomicInteger();
// 文件夹名修改数量
AtomicInteger floderRenameCount = new AtomicInteger();
// 文件名修改失败数量
AtomicInteger fileRenameCountFail = new AtomicInteger();
// 文件夹名修改失败数量
AtomicInteger floderRenameCountFail = new AtomicInteger();
try {
// 读取INI文件内容
Properties properties = new Properties();
properties.load(new InputStreamReader(new FileInputStream(iniFilePath), StandardCharsets.UTF_8));
// 获取旧文件名和新文件名列表
String oldNamesStr = properties.getProperty("oldFileName");
String newNamesStr = properties.getProperty("newFileName");
List<String> oldNames = Arrays.asList(oldNamesStr.split(","));
List<String> newNames = Arrays.asList(newNamesStr.split(","));
// 确保两个列表长度相同
if (oldNames.size() != newNames.size()) {
throw new IllegalArgumentException("Old names and new names lists must have the same length.");
}
// 遍历目标文件夹及其子文件夹中的所有文件夹并重命名
for (int i = 0; i < oldNames.size(); i++) {
String oldName = oldNames.get(i).trim();
String newName = newNames.get(i).trim();
Path oldFilePath = Paths.get(oldName);
Path newFilePath = Paths.get(newName);
try {
Files.move(oldFilePath, newFilePath, StandardCopyOption.REPLACE_EXISTING);
logMessage("Renamed file: " + oldName + " -> " + newName, logPath);
fileRenameCount.getAndIncrement();
} catch (IOException e) {
logMessage("Failed to rename file: " + oldName, logPath);
e.printStackTrace();
fileRenameCountFail.getAndIncrement();
}
}
// 获取旧文件名和新文件名列表
String oldDirectoryNamesStr = properties.getProperty("oldDirectoryFileName");
String newDirectoryNamesStr = properties.getProperty("newDirectoryFileName");
List<String> oldDirectoryNames = Arrays.asList(oldDirectoryNamesStr.split(","));
List<String> newDirectoryNames = Arrays.asList(newDirectoryNamesStr.split(","));
// 确保两个列表长度相同
if (oldDirectoryNames.size() != newDirectoryNames.size()) {
throw new IllegalArgumentException("Old names and new names lists must have the same length.");
}
// 遍历目标文件夹及其子文件夹中的所有文件夹并重命名
for (int i = 0; i < oldDirectoryNames.size(); i++) {
String oldDirectoryName = oldDirectoryNames.get(i);
String newDirectoryName = newDirectoryNames.get(i);
Path oldDirectoryPath = Paths.get(oldDirectoryName);
Path newDirectoryPath = Paths.get(newDirectoryName);
try {
Files.move(oldDirectoryPath, newDirectoryPath, StandardCopyOption.REPLACE_EXISTING);
System.out.println("Renamed directory: " + oldDirectoryName + " -> " + newDirectoryName);
logMessage("Renamed directory: " + oldDirectoryName + " -> " + newDirectoryName, logPath);
floderRenameCount.getAndIncrement();
} catch (IOException e) {
System.err.println("Failed to rename directory: " + oldDirectoryName);
logMessage("Failed to rename directory: " + oldDirectoryName, logPath);
e.printStackTrace();
floderRenameCountFail.getAndIncrement();
}
}
System.out.println("file rename count:" + fileRenameCount.get());
logMessage("file rename count:" + fileRenameCount.get(), logPath);
System.out.println("directory rename count:" + floderRenameCount.get());
logMessage("directory rename count:" + floderRenameCount.get(), logPath);
System.out.println("file rename failed count:" + fileRenameCountFail.get());
logMessage("file rename failed count:" + fileRenameCountFail.get(), logPath);
System.out.println("directory rename failed count:" + floderRenameCountFail.get());
logMessage("directory rename failed count:" + floderRenameCountFail.get(), logPath);
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
System.err.println(e.getMessage());
}
}
/**
* 根据文件全路径更新文件名
* 此处使用config.ini
*/
@GetMapping("/renameFileByFileName")
public void renameFileByFileName(){
// 指定INI文件路径和目标文件夹路径
// String iniFilePath = "F:\程序\code\file-rename\src\main\resources\config.ini";
// String logPath = "F:\程序\log.txt";
// 文件名修改数量
AtomicInteger fileRenameCount = new AtomicInteger();
// 文件夹名修改数量
AtomicInteger floderRenameCount = new AtomicInteger();
// 文件名修改失败数量
AtomicInteger fileRenameCountFail = new AtomicInteger();
// 文件夹名修改失败数量
AtomicInteger floderRenameCountFail = new AtomicInteger();
try {
// 读取INI文件内容
Properties properties = new Properties();
properties.load(new InputStreamReader(new FileInputStream(iniFilePath), StandardCharsets.UTF_8));
// 获取旧文件名和新文件名列表
String oldNamesStr = properties.getProperty("oldFileName");
String newNamesStr = properties.getProperty("newFileName");
List<String> oldNames = Arrays.asList(oldNamesStr.split(","));
List<String> newNames = Arrays.asList(newNamesStr.split(","));
// 确保两个列表长度相同
if (oldNames.size() != newNames.size()) {
throw new IllegalArgumentException("Old names and new names lists must have the same length.");
}
// 遍历目标文件夹及其子文件夹中的所有文件夹并重命名
for (int i = 0; i < oldNames.size(); i++) {
String oldName = oldNames.get(i).trim();
String newName = newNames.get(i).trim();
Path oldFilePath = Paths.get(oldName);
Path newFilePath = Paths.get(newName);
try {
Files.move(oldFilePath, newFilePath, StandardCopyOption.REPLACE_EXISTING);
logMessage("Renamed file: " + oldName + " -> " + newName, logPath);
fileRenameCount.getAndIncrement();
} catch (IOException e) {
logMessage("Failed to rename file: " + oldName, logPath);
e.printStackTrace();
fileRenameCountFail.getAndIncrement();
}
}
// 获取旧文件名和新文件名列表
String oldDirectoryNamesStr = properties.getProperty("oldDirectoryFileName");
String newDirectoryNamesStr = properties.getProperty("newDirectoryFileName");
List<String> oldDirectoryNames = Arrays.asList(oldDirectoryNamesStr.split(","));
List<String> newDirectoryNames = Arrays.asList(newDirectoryNamesStr.split(","));
// 确保两个列表长度相同
if (oldDirectoryNames.size() != newDirectoryNames.size()) {
throw new IllegalArgumentException("Old names and new names lists must have the same length.");
}
// 遍历目标文件夹及其子文件夹中的所有文件夹并重命名
for (int i = 0; i < oldDirectoryNames.size(); i++) {
String oldDirectoryName = oldDirectoryNames.get(i);
String newDirectoryName = newDirectoryNames.get(i);
Path oldDirectoryPath = Paths.get(oldDirectoryName);
Path newDirectoryPath = Paths.get(newDirectoryName);
try {
Files.move(oldDirectoryPath, newDirectoryPath, StandardCopyOption.REPLACE_EXISTING);
System.out.println("Renamed directory: " + oldDirectoryName + " -> " + newDirectoryName);
logMessage("Renamed directory: " + oldDirectoryName + " -> " + newDirectoryName, logPath);
floderRenameCount.getAndIncrement();
} catch (IOException e) {
System.err.println("Failed to rename directory: " + oldDirectoryName);
logMessage("Failed to rename directory: " + oldDirectoryName, logPath);
e.printStackTrace();
floderRenameCountFail.getAndIncrement();
}
}
System.out.println("file rename count:" + fileRenameCount.get());
logMessage("file rename count:" + fileRenameCount.get(), logPath);
System.out.println("directory rename count:" + floderRenameCount.get());
logMessage("directory rename count:" + floderRenameCount.get(), logPath);
System.out.println("file rename failed count:" + fileRenameCountFail.get());
logMessage("file rename failed count:" + fileRenameCountFail.get(), logPath);
System.out.println("directory rename failed count:" + floderRenameCountFail.get());
logMessage("directory rename failed count:" + floderRenameCountFail.get(), logPath);
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
System.err.println(e.getMessage());
}
}
/**
* 按照config.ini文件替换当前文件夹下文件夹以及文件的文件名
* 此处使用config1.ini
*/
@GetMapping("/rename")
public void renameFile(){
// 文件名修改数量
AtomicInteger fileRenameCount = new AtomicInteger();
// 文件夹名修改数量
AtomicInteger floderRenameCount = new AtomicInteger();
// 文件名修改失败数量
AtomicInteger fileRenameCountFail = new AtomicInteger();
// 文件夹名修改失败数量
AtomicInteger floderRenameCountFail = new AtomicInteger();
try {
// 读取INI文件内容
Properties properties = new Properties();
properties.load(new FileInputStream(iniFilePath));
// 获取旧文件名和新文件名列表
String oldNamesStr = properties.getProperty("oldName");
String newNamesStr = properties.getProperty("newName");
List<String> oldNames = Arrays.asList(oldNamesStr.split(","));
List<String> newNames = Arrays.asList(newNamesStr.split(","));
// 确保两个列表长度相同
if (oldNames.size() != newNames.size()) {
throw new IllegalArgumentException("Old names and new names lists must have the same length.");
}
// 遍历目标文件夹及其子文件夹中的所有文件并重命名
Files.walk(Paths.get(targetFolderPath))
.filter(Files::isRegularFile)
.forEach(oldFilePath -> {
String oldName = oldFilePath.getFileName().toString().trim();
String newName = oldName;
for (int i = 0; i < oldNames.size(); i++) {
newName = newName.replace(oldNames.get(i).trim(), newNames.get(i).trim());
}
if (!newName.equals(oldName)) {
Path newFilePath = oldFilePath.resolveSibling(newName);
try {
Files.move(oldFilePath, newFilePath, StandardCopyOption.REPLACE_EXISTING);
System.out.println("Renamed file: " + oldName + " -> " + newName);
logMessage("Renamed file: " + oldName + " -> " + newName, logPath);
fileRenameCount.getAndIncrement();
} catch (IOException e) {
System.err.println("Failed to rename file: " + oldName);
logMessage("Failed to rename file: " + oldName, logPath);
e.printStackTrace();
fileRenameCountFail.getAndIncrement();
}
} else {
System.out.println("No rename rule found for file: " + oldName);
logMessage("No rename rule found for file: " + oldName, logPath);
}
});
// 遍历目标文件夹及其子文件夹中的所有文件夹并重命名
Files.walk(Paths.get(targetFolderPath))
.filter(Files::isDirectory)
.sorted(Comparator.comparingInt((Path p) -> p.getNameCount()).reversed()) // 从最深的子目录开始重命名
.forEach(oldDirPath -> {
String oldName = oldDirPath.getFileName().toString().trim();
String newName = oldName;
for (int i = 0; i < oldNames.size(); i++) {
newName = newName.replace(oldNames.get(i).trim(), newNames.get(i).trim());
}
if (!newName.equals(oldName)) {
Path newDirPath = oldDirPath.resolveSibling(newName);
try {
Files.move(oldDirPath, newDirPath, StandardCopyOption.REPLACE_EXISTING);
System.out.println("Renamed directory: " + oldName + " -> " + newName);
logMessage("Renamed directory: " + oldName + " -> " + newName, logPath);
floderRenameCount.getAndIncrement();
} catch (IOException e) {
System.err.println("Failed to rename directory: " + oldName);
logMessage("Failed to rename directory: " + oldName, logPath);
e.printStackTrace();
floderRenameCountFail.getAndIncrement();
}
} else {
System.out.println("No rename rule found for directory: " + oldName);
logMessage("No rename rule found for directory: " + oldName, logPath);
}
});
System.out.println("file rename count:" + fileRenameCount.get());
logMessage("file rename count:" + fileRenameCount.get(), logPath);
System.out.println("directory rename count:" + floderRenameCount.get());
logMessage("directory rename count:" + floderRenameCount.get(), logPath);
System.out.println("file rename failed count:" + fileRenameCountFail.get());
logMessage("file rename failed count:" + fileRenameCountFail.get(), logPath);
System.out.println("directory rename failed count:" + floderRenameCountFail.get());
logMessage("directory rename failed count:" + floderRenameCountFail.get(), logPath);
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
System.err.println(e.getMessage());
}
}
/**
* 写日志
* @param message 日志信息
* @param logFilePath 日志文件路径
*/
private static void logMessage(String message, String logFilePath) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(logFilePath, true))) {
writer.write(message);
writer.newLine();
} catch (IOException e) {
e.printStackTrace();
}
}
}
application.yml
spring:
application:
name: file-rename
session:
timeout: 86400
server:
port: 8080
servlet:
context-path: /api
mybatis-plus:
configuration:
map-underscore-to-camel-case: false
global-config:
db-config:
logic-delete-field: isDelete
logic-delete-value: 1
logic-not-delete-value: 0
lwl:
# ini文件位置
iniFilePath: F:\file-rename\src\main\resources\config.ini
# 需要重命名文件夹位置
targetFolderPath: F:\tst-txt
# 日志存放位置
logPath: F:\log.txt
config.ini
[FileMapping]
oldFileName=F:/重命名/28/20231204/2024112601230060/2024112601230060-20231204173101.mp4, F:/重命名/29/20231204/2024112601236638/2024112601236638-20231204120100.mp4
newFileName=F:/重命名/28/20231204/2024112601230060/666666666666-20231204173101.mp4, F:/重命名/29/20231204/2024112601236638/888888888888-20231204120100.mp4
oldDirectoryFileName=F:/重命名/28/20231204/2024112601230060, F:/重命名/29/20231204/2024112601236638
newDirectoryFileName=F:/重命名/28/20231204/666666666, F:/重命名/29/20231204/888888888
config1.ini
[FileMapping]
oldName=2024112601230060, 2024112601236638
newName=888888888888, 666666666666