我的脚本突然出现以下错误:
if (!file.isFile()) {
^
TypeError: file.isFile is not a function
对为什么有任何想法?目录正确。 (下面的完整脚本):
const fs = require('fs')
const path = require('path')
let dir = './'
fs.readdir(dir, {
encoding: 'utf8',
withFileTypes: true
}, (err, files) => {
if (err) {
console.error(err)
return
}
for (const file of files) {
if (!file.isFile()) { // Here is where error is occurring
// Directory or block device?
continue;
}
// Read file contents
let fileContents = fs.readFileSync(path.join(dir, file.name), 'utf8')
let dateMatches = fileContents.match(/[12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])/)
if (dateMatches) {
// There is a date
fs.rename(path.join(dir, file.name), path.join(dir, dateMatches[0]), (err) => {
if (err) {
console.error(err)
// error handling
return
} else {
console.log('Renamed', file.name, 'to', dateMatches[0])
}
})
}
}
})