Created
May 10, 2020 14:55
-
-
Save daveschumaker/c69490da18b3a24f906cd86ae364c0bf to your computer and use it in GitHub Desktop.
Rename all files in 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
// 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