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
| const fs = require('fs') const path = require('path') const directoryPath = __dirname
fs.readdir(directoryPath, (err, files) => { if (err) { return console.log('无法扫描目录: ' + err) } files.filter(file => path.extname(file) === '.ts').forEach(file => { const fileNameWithoutExt = path.basename(file, '.ts') const index = fileNameWithoutExt.indexOf('|') if (index !== -1) { const newFileName = fileNameWithoutExt.substring(0, index) + '.ts' const oldFilePath = path.join(directoryPath, file) const newFilePath = path.join(directoryPath, newFileName) fs.rename(oldFilePath, newFilePath, err => { if (err) { console.log('重命名文件时出错: ' + err) } else { console.log(`重命名: ${file} -> ${newFileName}`) } }) } }) })
|