Rename File Skill
This skill renames all files in a specified directory by adding a prefix to each filename. It shows a preview of changes and asks for user confirmation before executing the rename operation.
When to use this skill
Use this skill when:
- - User wants to batch rename files in a directory
- User needs to add a consistent prefix to multiple files
- User wants to preview changes before applying them
- User wants a safe, interactive file renaming tool
Prerequisites
The skill uses a JavaScript script for file operations, which requires Node.js to be installed.
Implementation steps
1. Ask for user inputs
Use AskUserQuestion to get:
- 1. Prefix: What prefix should be added to the filenames?
- Directory path: Which directory contains the files to rename? (Enter absolute or relative path)
- Confirmation: Show preview and ask for confirmation before proceeding
Example AskUserQuestion call for prefix and directory:
CODEBLOCK0
2. Process user inputs
After getting answers:
- - Prefix: Use the user-provided prefix (sanitize if needed)
- Directory path:
- If "Current directory" selected, use current working directory
- If "Custom path" selected, ask user to enter the path via a follow-up question
- Validate that the directory exists and is readable
3. Generate preview
Read the directory contents and generate a preview of rename operations:
- - List all files (excluding directories) in the target directory
- Show current filename → new filename mapping
- Count total files to be renamed
Display the preview to the user and ask for confirmation:
CODEBLOCK1
4. Execute rename operation
If user confirms, run the JavaScript rename script:
CODEBLOCK2
5. Display results
Show the user:
- - Number of files successfully renamed
- Any errors encountered
- Summary of changes made
Script details
The JavaScript script (scripts/rename-files.js):
- 1. Validates the directory exists and is accessible
- Reads all files in the directory (excluding subdirectories)
- For each file, constructs new filename by prepending the prefix
- Performs the rename operation using INLINECODE2
- Handles errors gracefully (e.g., permission issues, duplicate names)
- Returns success/failure status
Error handling
- - If directory doesn't exist or is not accessible, show error and abort
- If no files found in directory, inform user and exit
- If a file with the new name already exists, skip that file and report
- If permission denied for any file, skip and report error
- Rollback on critical errors (optional, can be implemented)
Safety features
- - Always shows preview before making changes
- Asks for explicit confirmation
- Skips directories (only renames files)
- Preserves file extensions
- Reports all operations for transparency
Usage example
CODEBLOCK3
重命名文件技能
此技能通过为每个文件名添加前缀来重命名指定目录中的所有文件。在执行重命名操作前,它会显示更改预览并请求用户确认。
何时使用此技能
在以下情况下使用此技能:
- - 用户想要批量重命名目录中的文件
- 用户需要为多个文件添加一致的前缀
- 用户希望在应用更改前预览更改
- 用户需要一个安全、交互式的文件重命名工具
前提条件
该技能使用JavaScript脚本进行文件操作,需要安装Node.js。
实施步骤
1. 询问用户输入
使用AskUserQuestion获取:
- 1. 前缀:应该添加到文件名中的前缀是什么?
- 目录路径:包含要重命名文件的目录是哪个?(输入绝对或相对路径)
- 确认:显示预览并在继续前请求确认
前缀和目录的AskUserQuestion调用示例:
json
{
questions: [{
question: 应该为文件名添加什么前缀?,
header: 前缀,
options: [
{label: 输入自定义前缀, description: 我将输入一个前缀(例如:photo、backup)}
],
multiSelect: false
}, {
question: 哪个目录包含要重命名的文件?,
header: 目录,
options: [
{label: 当前目录, description: 使用当前工作目录},
{label: 自定义路径, description: 我将指定其他目录}
],
multiSelect: false
}]
}
2. 处理用户输入
获取答案后:
- - 前缀:使用用户提供的前缀(如有需要则进行清理)
- 目录路径:
- 如果选择了当前目录,则使用当前工作目录
- 如果选择了自定义路径,则通过后续问题询问用户输入路径
- 验证目录存在且可读
3. 生成预览
读取目录内容并生成重命名操作预览:
- - 列出目标目录中的所有文件(排除目录)
- 显示当前文件名 → 新文件名的映射
- 统计将要重命名的文件总数
向用户显示预览并请求确认:
json
{
questions: [{
question: 预览:${count}个文件将使用前缀${prefix}进行重命名。是否继续?,
header: 确认,
options: [
{label: 是,重命名文件, description: 执行重命名操作},
{label: 否,取消, description: 取消,不进行任何更改}
],
multiSelect: false
}]
}
4. 执行重命名操作
如果用户确认,运行JavaScript重命名脚本:
bash
获取此技能目录的路径
SKILL_DIR=$(dirname $0)/rename-file
运行JavaScript重命名脚本
node $SKILL_DIR/scripts/rename-files.js <前缀> <目录路径>
5. 显示结果
向用户显示:
- - 成功重命名的文件数量
- 遇到的任何错误
- 所做更改的摘要
脚本详情
JavaScript脚本(scripts/rename-files.js):
- 1. 验证目录存在且可访问
- 读取目录中的所有文件(排除子目录)
- 为每个文件通过添加前缀构建新文件名
- 使用fs.renameSync执行重命名操作
- 优雅地处理错误(例如:权限问题、重复名称)
- 返回成功/失败状态
错误处理
- - 如果目录不存在或无法访问,显示错误并中止
- 如果在目录中未找到文件,通知用户并退出
- 如果新名称的文件已存在,跳过该文件并报告
- 如果任何文件权限被拒绝,跳过并报告错误
- 关键错误时回滚(可选,可实施)
安全特性
- - 在进行更改前始终显示预览
- 请求明确确认
- 跳过目录(仅重命名文件)
- 保留文件扩展名
- 报告所有操作以确保透明度
使用示例
用户:/rename-file
助手:询问前缀和目录
用户:前缀:vacation_,目录:./photos
助手:显示预览:photo1.jpg → vacation_photo1.jpg等
用户:确认
助手:重命名文件并显示摘要