const fs = require('fs')
const path = require('path')
function getBirthTime(dir) {
let stat = fs.statSync(dir)
return stat.birthtimeMs
}
function isValid(filename) {
return filename.startsWith('T')
}
const filesPath = path.join(__dirname, '/mm')
fs.readdir(filesPath, (err, files) => {
if (!err){
files.sort((x, y) => {
let xp = path.join(filesPath, x)
let yp = path.join(filesPath, y)
return getBirthTime(yp) - getBirthTime(xp)
})
console.log(files)
let curValidFile
let curValidName
let cnt = 1
files.forEach(f => {
console.log(f)
if (isValid(f)){
curValidName = f
curValidFile = path.join(filesPath, f)
cnt = 1
}else {
let newname = `${curValidName.slice(0, -4)}--r--${cnt++}.pdf`
let newpath = path.join(filesPath, newname)
fs.renameSync(path.join(filesPath, f), newpath)
}
})
}
})