需求将imgs文件下的图片重命名为规则的命名方式
let fs = require('fs') let path = require('path')//获取文件位置 let url = path.join(__dirname,'/imgs/')
fs.readdir(url, (err, files) => { files.forEach((item,index) => { //rename将旧文件名替换为新文件名 fs.rename( path.join(url, item), path.join(url, 'prefix_' + index+'.png'), err => { if (err) throw err console.log(`${item} & prefix_${item}`) } ) }) }) 标签:node,重命名,fs,err,url,item,let,名称,path From: https://www.cnblogs.com/lijun12138/p/17187469.html