Skip to content

Instantly share code, notes, and snippets.

@nibble-4bits
Created August 24, 2021 17:51
Show Gist options
  • Save nibble-4bits/fe3ec63eeedda0519ca0a634d0a17977 to your computer and use it in GitHub Desktop.
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
#!/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