批量修改文件


对于一些文件命名有重命名的需求,特写了一个node批量处理程序

为了方便后续管理,拆分成了两个文件

一个程序文件(index.js)和一个执行文件(main.js)

index.js

import * as fs from 'fs';

export default {

	renameFilesByPath: function () {
        //这里没办法使用绝对路径
		const path = '../../Note';
        
		const dirArr = [];
		// 被替换的内容
		let oldStr = '1_';
		// 替换的新内容
		let newStr = '';

		//同步获取文件
		const files = fs.readdirSync(path);
		//批量处理
		files.forEach((file, index) => {
			if (file.includes(oldStr)) {
				let newName = file.replace(oldStr, newStr)
				fs.rename(`${path}/${files[index]}`, `${path}/${newName}`, (err) => {
					if (!err) {
						console.log(newName + '重命名成功')
					} else {
						console.log(err)
					}
				})
			}
		});
	}

}

main.js

import tools from "./tools/index.js";

tools.renameFilesByPath();

文章作者: iamfugui
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 iamfugui !
评论
  目录