Created
August 24, 2021 17:51
-
-
Save nibble-4bits/fe3ec63eeedda0519ca0a634d0a17977 to your computer and use it in GitHub Desktop.
Small NodeJS script to recursively rename the extension of files inside a directory
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
#!/usr/bin/env node | |
const { sync: globSync } = require('glob'); | |
const { moveSync } = require('fs-extra'); | |
const { exit } = require('process'); | |
const dir = process.argv[2]; | |
const oldExt = process.argv[3]; | |
const newExt = process.argv[4]; | |
if (!dir || !oldExt || !newExt) { | |
console.log(`USAGE: renameExt [directory] [old extension] [new extension]`); | |
exit(1); | |
} | |
const files = globSync(`${dir}/**/*.${oldExt}`); | |
for (const file of files) { | |
const fileWithoutExtension = file.slice(0, file.indexOf(`.${oldExt}`)); | |
moveSync(file, `${fileWithoutExtension}.${newExt}`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment