Skip to content

Commit 23a0ed4

Browse files
author
linyuan.yang
committed
重命名工具
1 parent 665a842 commit 23a0ed4

11 files changed

Lines changed: 1076 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### 新增
66

7+
- 新增批量重命名工具:可添加文件(支持拖入窗口与选择文件夹),按顺序叠加多条规则(搜索替换、插入文本、序号编号、大小写转换、删除)实时预览最终文件名,支持逐步预览、规则拖拽排序与冲突检测后一键执行。
78
- JSON 树状查看器中,长字符串默认截断为单行展示,可一键展开查看完整内容;字符串值本身是 JSON(对象 / 数组)时可内联解析为子树查看。
89

910
### 优化

‎packages/app/src-tauri/src/lib.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ pub fn run() {
6464
tools::hosts::hosts_read,
6565
tools::hosts::hosts_write,
6666
tools::file_locks::file_locks_check,
67+
tools::batch_rename::batch_rename_list_dir,
68+
tools::batch_rename::batch_rename_is_dir,
69+
tools::batch_rename::batch_rename_execute,
6770
tools::port_check::port_check_list,
6871
tools::port_check::port_check_kill,
6972
tools::keystore_gen::keystore_check_java,
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
#[derive(Serialize)]
4+
#[serde(rename_all = "camelCase")]
5+
pub struct DirEntry {
6+
pub name: String,
7+
pub path: String,
8+
pub is_dir: bool,
9+
}
10+
11+
#[derive(Deserialize)]
12+
#[serde(rename_all = "camelCase")]
13+
pub struct RenameRequestItem {
14+
pub from: String,
15+
pub to: String,
16+
}
17+
18+
#[derive(Serialize)]
19+
#[serde(rename_all = "camelCase")]
20+
pub struct RenameResultItem {
21+
pub from: String,
22+
pub to: String,
23+
/// "renamed" | "skipped" | "error"
24+
pub status: String,
25+
pub error: Option<String>,
26+
}
27+
28+
#[derive(Serialize)]
29+
#[serde(rename_all = "camelCase")]
30+
pub struct RenameBatchResult {
31+
pub items: Vec<RenameResultItem>,
32+
pub renamed: usize,
33+
pub skipped: usize,
34+
pub failed: usize,
35+
}
36+
37+
/// 判断路径是否为文件夹(拖拽进来的路径需要过滤掉目录)。
38+
#[tauri::command(rename_all = "camelCase")]
39+
pub async fn batch_rename_is_dir(path: String) -> Result<bool, String> {
40+
Ok(std::path::Path::new(&path).is_dir())
41+
}
42+
43+
/// 列出文件夹的直接子项(含子目录,不递归),按文件名排序。
44+
#[tauri::command(rename_all = "camelCase")]
45+
pub async fn batch_rename_list_dir(path: String) -> Result<Vec<DirEntry>, String> {
46+
tauri::async_runtime::spawn_blocking(move || list_dir(path))
47+
.await
48+
.map_err(|error| format!("读取文件夹任务失败: {error}"))?
49+
}
50+
51+
fn list_dir(path: String) -> Result<Vec<DirEntry>, String> {
52+
let entries = std::fs::read_dir(&path).map_err(|error| format!("无法读取文件夹 {path}: {error}"))?;
53+
let mut items: Vec<DirEntry> = Vec::new();
54+
for entry in entries.flatten() {
55+
let name = entry.file_name().to_string_lossy().to_string();
56+
let is_dir = entry.file_type().map(|t| t.is_dir()).unwrap_or(false);
57+
items.push(DirEntry {
58+
path: entry.path().to_string_lossy().to_string(),
59+
name,
60+
is_dir,
61+
});
62+
}
63+
if items.is_empty() {
64+
return Err(format!("文件夹为空或无法读取内容: {path}"));
65+
}
66+
items.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase()));
67+
Ok(items)
68+
}
69+
70+
/// 批量执行重命名。逐项独立处理,单项失败不中断批次;整体只在参数为空时报错。
71+
#[tauri::command(rename_all = "camelCase")]
72+
pub async fn batch_rename_execute(items: Vec<RenameRequestItem>) -> Result<RenameBatchResult, String> {
73+
if items.is_empty() {
74+
return Err("没有需要重命名的文件".into());
75+
}
76+
tauri::async_runtime::spawn_blocking(move || execute_batch(items))
77+
.await
78+
.map_err(|error| format!("重命名任务失败: {error}"))?
79+
}
80+
81+
fn execute_batch(items: Vec<RenameRequestItem>) -> Result<RenameBatchResult, String> {
82+
let mut results: Vec<RenameResultItem> = Vec::with_capacity(items.len());
83+
let mut renamed = 0usize;
84+
let mut skipped = 0usize;
85+
let mut failed = 0usize;
86+
87+
for item in items {
88+
let result = rename_one(&item);
89+
match result.status.as_str() {
90+
"renamed" => renamed += 1,
91+
"skipped" => skipped += 1,
92+
_ => failed += 1,
93+
}
94+
results.push(result);
95+
}
96+
97+
Ok(RenameBatchResult {
98+
items: results,
99+
renamed,
100+
skipped,
101+
failed,
102+
})
103+
}
104+
105+
fn rename_one(item: &RenameRequestItem) -> RenameResultItem {
106+
let make_error = |message: String| RenameResultItem {
107+
from: item.from.clone(),
108+
to: item.to.clone(),
109+
status: "error".into(),
110+
error: Some(message),
111+
};
112+
113+
let from = std::path::Path::new(&item.from);
114+
let to = std::path::Path::new(&item.to);
115+
116+
if !from.exists() {
117+
return make_error(format!("源文件不存在: {}", item.from));
118+
}
119+
120+
// Windows 文件系统不区分大小写:完全一致视为无变化(跳过),
121+
// 仅大小写差异视为合法的就地改名(NTFS 支持),其余情况目标已存在则报错
122+
if item.from == item.to {
123+
return RenameResultItem {
124+
from: item.from.clone(),
125+
to: item.to.clone(),
126+
status: "skipped".into(),
127+
error: Some("文件名无变化".into()),
128+
};
129+
}
130+
131+
let case_only = item.from.eq_ignore_ascii_case(&item.to);
132+
if !case_only && to.symlink_metadata().is_ok() {
133+
// Rust 的 fs::rename 在 Windows 上带 MOVEFILE_REPLACE_EXISTING,会静默覆盖已存在文件,
134+
// 必须显式预检(symlink_metadata 同时能发现悬空符号链接)
135+
return make_error(format!("目标名已存在: {}", item.to));
136+
}
137+
138+
match std::fs::rename(from, to) {
139+
Ok(()) => RenameResultItem {
140+
from: item.from.clone(),
141+
to: item.to.clone(),
142+
status: "renamed".into(),
143+
error: None,
144+
},
145+
Err(error) => {
146+
let mut message = format!("重命名失败: {error}");
147+
// 目标被其他进程占用(如正在被编辑器/资源管理器打开)是常见失败原因
148+
if error.raw_os_error() == Some(32) {
149+
message.push_str(";文件可能正被其他程序占用,可用「文件占用检查」工具排查");
150+
}
151+
make_error(message)
152+
}
153+
}
154+
}

‎packages/app/src-tauri/src/tools/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod autostart;
2+
pub mod batch_rename;
23
pub mod env_vars;
34
pub mod external;
45
pub mod gdrive_login;

‎packages/app/src/shell/registry.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { systemInfoTool } from '../tools/account/system-info'
88
import { xiaoaiLoginTool } from '../tools/account/xiaoai-login'
99
import { gdriveLoginTool } from '../tools/account/gdrive-login'
1010
import { fileLocksTool } from '../tools/devtool/file-locks'
11+
import { batchRenameTool } from '../tools/file/batch-rename'
1112
import { portCheckTool } from '../tools/devtool/port-check'
1213
import { envVarsTool } from '../tools/devtool/env-vars'
1314
import { translatorTool } from '../tools/devtool/translator'
@@ -19,6 +20,7 @@ import { screenshotTool } from '../tools/media/screenshot'
1920
export const NATIVE_TOOLS: ToolDef[] = [
2021
systemInfoTool,
2122
fileLocksTool,
23+
batchRenameTool,
2224
portCheckTool,
2325
envVarsTool,
2426
screenshotTool,

0 commit comments

Comments
 (0)