🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

daji

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

daji - npm Package Compare versions

Comparing version
2.0.0
to
2.0.1
+98
doc/rename.md
# 文件重命名模块 (rename.js)
## 1. 模块概述
文件重命名模块用于批量重命名当前目录下的文件,按照指定前缀和递增序号的格式进行重命名,适用于整理下载的图片或其他文件。
## 2. 功能说明
| 功能 | 描述 |
|------|------|
| 批量重命名 | 批量重命名当前目录下的所有文件 |
| 自定义前缀 | 可以自定义文件名前缀,默认P |
| 递增序号 | 按照P00001、P00002的格式生成序号 |
| 保留扩展名 | 保留原文件的扩展名 |
| 输出日志 | 显示重命名过程和结果 |
## 3. 命令格式
```bash
daji rename [options]
# 或
daji rn [options]
```
## 4. 选项说明
| 选项 | 缩写 | 参数 | 描述 |
|------|------|------|------|
| --prefix | -p | 字符串 | 自定义前缀,默认P |
## 5. 使用示例
### 5.1 使用默认前缀重命名
```bash
daji rename
# 或
daji rn
```
执行此命令后,工具会将当前目录下的文件按照P00001、P00002的格式重命名。
### 5.2 使用自定义前缀重命名
```bash
daji rename -p IMG
# 或
daji rn -p IMG
```
执行此命令后,工具会将当前目录下的文件按照IMG00001、IMG00002的格式重命名。
## 6. 实现原理
1. **获取文件列表**:
- 读取当前目录下的所有文件
- 过滤出文件,排除目录
- 按文件名排序
2. **构建新文件名**:
- 使用指定前缀(默认P)
- 生成5位递增序号(不足补零)
- 保留原文件的扩展名
3. **执行重命名**:
- 遍历文件列表
- 逐个执行重命名操作
- 输出重命名结果
4. **统计结果**:
- 统计成功和失败的文件数量
- 输出最终结果
## 7. 输出示例
```
当前目录: /Users/user/Downloads/images
找到 5 个文件,准备重命名...
==============================
DSC_0001.jpg → P00001.jpg
DSC_0002.jpg → P00002.jpg
DSC_0003.jpg → P00003.jpg
DSC_0004.jpg → P00004.jpg
DSC_0005.jpg → P00005.jpg
==============================
重命名完成!成功: 5 个,失败: 0 个
```
## 8. 注意事项
- 该命令会重命名当前目录下的**所有文件**,请谨慎使用
- 建议在执行前先备份重要文件
- 文件名会按字母顺序排序后再重命名
- 命令执行后无法撤销,请确保操作正确
## 9. 依赖关系
- 无外部依赖,仅使用Node.js内置的`fs`和`path`模块
const fs = require('fs').promises;
const path = require('path');
const utils = require('../common/utils');
const log = utils.msg;
/**
* 重命名当前目录下的文件
* @param {string} cmd - 命令参数
* @param {object} options - 命令选项
*/
async function renameTools(cmd, options) {
const { prefix = 'P' } = options;
try {
// 获取当前目录
const currentDir = process.cwd();
log.info(`当前目录: ${currentDir}`);
// 获取当前目录下的所有文件
const files = await fs.readdir(currentDir, { withFileTypes: true });
// 过滤出文件(排除目录)
const fileList = files
.filter(file => file.isFile())
.map(file => file.name)
.sort(); // 按文件名排序
if (fileList.length === 0) {
log.tip('当前目录下没有文件需要重命名');
return;
}
log.info(`找到 ${fileList.length} 个文件,准备重命名...`);
console.log('==============================');
// 开始重命名
let successCount = 0;
let failCount = 0;
for (let i = 0; i < fileList.length; i++) {
const oldName = fileList[i];
const ext = path.extname(oldName);
const newName = `${prefix}${String(i + 1).padStart(5, '0')}${ext}`;
try {
await fs.rename(path.join(currentDir, oldName), path.join(currentDir, newName));
log.success(`${oldName} → ${newName}`);
successCount++;
} catch (error) {
log.error(`重命名 ${oldName} 失败: ${error.message}`);
failCount++;
}
}
console.log('==============================');
log.info(`重命名完成!成功: ${successCount} 个,失败: ${failCount} 个`);
} catch (error) {
log.error(`重命名过程中发生错误: ${error.message}`);
}
}
module.exports = renameTools;
+12
-0

@@ -14,2 +14,3 @@ #!/usr/bin/env node

const password = require('../lib/password');
const rename = require('../lib/rename');
const color = require('colors-cli/toxic');

@@ -133,2 +134,13 @@

// rename 命令
program
.command('rename')
.alias('rn')
.description('文件重命名工具'.x29)
.option('-p, --prefix <string>', '自定义前缀,默认P', 'P')
.action((options, command) => {
const cmd = command.args[0];
rename(cmd, options);
});
// 默认显示帮助

@@ -135,0 +147,0 @@ if (!process.argv[2]) {

+1
-0

@@ -136,1 +136,2 @@ # Daji 项目架构说明

- [password.md](password.md) - 密码生成模块
- [rename.md](rename.md) - 文件重命名模块
+1
-1
{
"name": "daji",
"version": "2.0.0",
"version": "2.0.1",
"description": "",

@@ -5,0 +5,0 @@ "main": "index.js",