Skip to content

Instantly share code, notes, and snippets.

@daveschumaker
Created May 10, 2020 14:55
Show Gist options
  • Save daveschumaker/c69490da18b3a24f906cd86ae364c0bf to your computer and use it in GitHub Desktop.
Save daveschumaker/c69490da18b3a24f906cd86ae364c0bf to your computer and use it in GitHub Desktop.
Rename all files in a directory
// Copy into directory you want to rename.
const fs = require('fs');
const BASE_NAME = 'BaseFile_Name';
const FILE_EXT = 'm4b';
fs.readdir('./', (err, files) => {
// Filter files by filetype, so we can ignore rename function.
const filteredFiles = files.filter((file, i) => {
return file.indexOf(FILE_EXT) > -1;
});
console.log(filteredFiles);
filteredFiles.forEach((file, i) => {
let formatNumber = i + 1;
if (formatNumber < 10) {
formatNumber = '0' + formatNumber;
}
fs.rename('./' + file, './' + BASE_NAME + formatNumber + '.m4b', function (
err
) {
if (err) console.log('ERROR: ' + err);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment