Blog
首页
文档
收藏
关于
  • 在线转换时间戳 (opens new window)
  • 在线压缩图片 (opens new window)
  • Float-Double转二进制 (opens new window)
  • 文件转Hex字符串 (opens new window)

HiuZing

🍑
首页
文档
收藏
关于
  • 在线转换时间戳 (opens new window)
  • 在线压缩图片 (opens new window)
  • Float-Double转二进制 (opens new window)
  • 文件转Hex字符串 (opens new window)
  • 前端面试题

  • JavaScript

  • Vue2

  • port

  • CSS

  • Node.js

    • nvm

    • Node.js实例

      • Node.js-批量更改文件名序号
        • 批量更改文件名序号(xxx.xxxx)
          • 场景:
      • express实例
    • Pm2

    • npm

    • 教程

  • JavaScript优化

  • uniapp

  • Mini Program

  • TypeScript

  • 面向对象编程

  • UI组件

  • Plugin

  • Vue3

  • 性能优化

  • Axios

  • 状态管理

  • React

  • Mock

  • Icon

  • Template

  • 构建工具

  • 项目规范配置

  • Taro

  • SVG

  • React Native

  • 前端
  • Node.js
  • Node.js实例
HiuZing
2022-08-23
目录

Node.js-批量更改文件名序号

# 批量更改文件名序号(xxx.xxxx)

# 场景:

在使用vuepress写文章的时候,遇到文件序号的文件问题——文件夹内文件名的序号,没有按照顺序排列

使用nodejs编写脚本解决

实现如下:

var fs = require("fs");

// 接收参数(文件夹路径)
var args = process.argv.slice(2);
if (args.length === 0) {
    console.log("请输入文件夹路径");
    return;
}
var path = args[0];

fs.readdir(path,function(err, files){
    if (err) {
        return console.error(err);
    }
    files.forEach(function (fileName, index) {
        // 索引加1,因为文件夹索引是从0开始的
        const fileNameindex = index + 1;
        // 新序号
        const number = fileNameindex.toString().padStart(2, '0')

        // 提取文件名.及後面的字符串
        const startIndex = fileName.indexOf('.')
        const newName = number + fileName.substring(startIndex)

        // 修改文件名
        fs.rename(`${path}/${fileName}`, `${path}/${newName}`, function (err) {
            if (err) throw err;
            console.log('renamed complete');
        });
    });
});

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
上次更新: 2024/08/14, 04:14:33
nvm介绍
express实例

← nvm介绍 express实例→

最近更新
01
React Native 使用SVG
08-13
02
Docker基础命令
08-04
03
算数逻辑单元
07-30
更多文章>
Theme by Vdoing | Copyright © 2021-2024 WeiXiaojing | 友情链接
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式