小布工坊.
← Back to the library

Resource details · 实用工具

先看照片批量改名后的样子

输入几个文件名,得到“原文件名 → 新文件名”的预览,不真正改动照片。

Resource text, examples and licences retain their original language. Code is displayed only; this page does not execute it or call AI.

Resource details

学习编号和字符串拼接,也能先规划命名。这里只给预览,不提供扫描文件夹或执行改名的功能。

Before you start

Platforms
Windows · macOS · Linux
Content updated
  • 先安装 Node.js 24,并准备可以保存纯文本的编辑器。
  • 把资源保存为 rename-preview.mjs;首次运行只用代码里自带的示例文字。
node rename-preview.mjs

需先安装 Node.js。无需 npm install;本例不联网、不读写用户文件。

Common pitfalls

  • Windows 开启文件扩展名显示,避免保存成 .mjs.txt;教程的文件夹开终端步骤以 Windows 为例。
  • 数组顺序决定编号。仅支持普通带扩展名的文件名,不接受完整路径;预览没有检查目标文件夹里的名称冲突,不要直接当成执行改名脚本。
Official setup guide ↗

Complete resource

// SPDX-License-Identifier: MIT
// 原创教学示例:仅生成改名前后对照,不读取磁盘,也不真正重命名。
export function planNames(names, prefix = "旅行") {
  if (!Array.isArray(names) || names.length > 100) throw new TypeError("文件名数组最多100项");
  if (typeof prefix !== "string" || !/^[\p{L}\p{N}_-]{1,30}$/u.test(prefix)) throw new TypeError("前缀只用文字、数字、短横线或下划线");
  const seen = new Set();
  return names.map((name, index) => {
    if (typeof name !== "string" || !/^[^<>:"/\\|?*\u0000-\u001f]{1,120}\.[A-Za-z0-9]{1,10}$/.test(name)) throw new TypeError("仅接受带扩展名的普通文件名,不接受路径");
    const key = name.toLowerCase();
    if (seen.has(key)) throw new Error("有重复文件名,请先核对");
    seen.add(key);
    const extension = name.slice(name.lastIndexOf("."));
    return { before: name, after: prefix + "-" + String(index + 1).padStart(3, "0") + extension };
  });
}

console.log(JSON.stringify(planNames(["IMG_0721.jpg", "IMG_0722.png"])));

How to use it

先运行资源中自带的小样本,看到结果以后,再试着改一两个值。

  1. 下载并保存示例

    下载代码,保存为 rename-preview.mjs。Windows 在资源管理器打开“查看 → 显示 → 文件扩展名”,确认没有多出 .txt。

  2. 运行一次

    在这个文件夹的地址栏输入 powershell 并回车,打开终端后输入下面的命令。若提示找不到 node,安装 Node.js 后重新打开终端。

  3. 对照结果再改一个值

    核对编号001、002和原扩展名;改前缀、数组里的文件名后再运行。磁盘中的文件名不会变化。

Save as
rename-preview.mjs
Environment
先安装 Node.js 24,并准备记事本或代码编辑器。本站实际核验版本为 v24.19.0。
Dependencies and costs
无需账号或API密钥;本例不联网,不读写用户文件,不产生模型接口费用。设备和电力仍有成本。

Command

node rename-preview.mjs

Expected output

[{"before":"IMG_0721.jpg","after":"旅行-001.jpg"},{"before":"IMG_0722.png","after":"旅行-002.png"}]

Local verification

2026-10-23 · Node.js v24.19.0

本站本地运行了下载时附带完整许可的原样示例,核对演示输出与注明的边界输入;不代表所有设备或生产场景验证。

Suitable uses

  • 第一次保存并运行 JavaScript 小例子
  • 先看照片批量改名后的样子

Before using it

  • 数组顺序决定编号。仅支持普通带扩展名的文件名,不接受完整路径;预览没有检查目标文件夹里的名称冲突,不要直接当成执行改名脚本。
  • 这是教学小片段;若要接入真实数据或改动文件,先另行设计与核验,不要把示例当成现成完整应用。

Authors and sources

Attribution
包子海梦 · AI辅助开发
Authors and sources
提示集贡献者

Notices and modifications

新编写的通用教学示例,发布前仍需核对来源与运行效果。

Complete licence

MIT

MIT License

Copyright (c) 2026 提示集贡献者

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Verification scope

教学代码已运行

已运行完整带许可的教学文件并核对输出;不代表生产部署、平台接入或全部边界情况都已验证。

2026-10-23 Node.js v24.19.0