Last active
November 14, 2022 12:33
-
-
Save EndBug/f1ca4cbaf2afd9a69a9d20ba6b8f31d9 to your computer and use it in GitHub Desktop.
Simple node script to bulk rename files and folders from "123 DD-MM-YYY Long title" to "YYYY-MM-DD Long title"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 123 DD-MM-YYY Long title -> YYYY-MM-DD Long title | |
const path = require('path') | |
const fs = require('fs') | |
const parentDir = path.resolve('RESOLVABLE_PATH') | |
const type = 'FILE' // FOLDER or FILE | |
const entities = fs.readdirSync(parentDir, { withFileTypes: true }) | |
.filter(dirent => type == 'FOLDER' ? dirent.isDirectory() : dirent.isFile()) | |
.map(dirent => dirent.name) | |
for (const ent of entities) { | |
const { name, ext } = path.parse(ent) | |
const words = name.split(' ') | |
console.log(ent); | |
if (words[0].length !== 3) | |
continue | |
const title = words.slice(2).join(' ') | |
const [DD, MM, YYYY] = words[1].split('-') | |
let newName = `${YYYY}-${MM}-${DD} ${title}`.trim() | |
if (ext) newName += ext | |
console.log(newName) | |
const oldPath = path.join(parentDir, ent) | |
const newPath = path.join(parentDir, newName) | |
fs.renameSync(oldPath, newPath) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment