Created
April 12, 2022 15:10
-
-
Save Aleksey-Danchin/35a39f30f8cf3ad1a313e0db81536885 to your computer and use it in GitHub Desktop.
Rename script for videos
This file contains 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
const { readdir, rename } = require("fs/promises"); | |
const { resolve } = require("path"); | |
(async () => { | |
const __dirname = resolve(); | |
const tracks = (await readdir(__dirname)) | |
.map((name) => { | |
const match = name.match(/(\d{4})-(\d{2})-(\d{2}) (\d{2})-(\d{2})-(\d{2}).mp4/i); | |
if (match) { | |
const date = new Date(match[1], match[2], match[3], match[4], match[5], match[6]); | |
return { | |
name, | |
date, | |
}; | |
} | |
}) | |
.filter((x) => x) | |
.sort((a, b) => a.date.getTime() - b.date.getTime()) | |
.map((obj, i) => ({ order: i + 1, ...obj })) | |
.map((obj) => ({ | |
...obj, | |
oldPath: resolve(__dirname, obj.name), | |
newPath: resolve(__dirname, `${obj.order}.mp4`), | |
})); | |
await Promise.all(tracks.map(({ oldPath, newPath }) => rename(oldPath, newPath))); | |
tracks.map(({ oldPath, newPath }) => console.log(`${oldPath} -> ${newPath}`)); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment