Skip to content

Instantly share code, notes, and snippets.

@StianWiu
Created November 13, 2021 18:29
Show Gist options
  • Save StianWiu/4eae28124a40c1a617d8260f3c470ba6 to your computer and use it in GitHub Desktop.
Save StianWiu/4eae28124a40c1a617d8260f3c470ba6 to your computer and use it in GitHub Desktop.
Renames all files with certain file extension with increasing number on the end.
//requiring path and fs modules
const path = require("path");
const fs = require("fs");
//joining path of directory
const directoryPath = path.join(__dirname, "/");
//passsing directoryPath and callback function
fs.readdir(directoryPath, function (err, files) {
//handling error
if (err) {
return console.log("Unable to scan directory: " + err);
}
//listing all files using forEach
let a = 1;
files.forEach(function (file) {
// Do whatever you want to do with the file
if (file.endsWith(".mp4")) {
console.log(file);
fs.rename(
directoryPath + file,
directoryPath + "Filename-" + a++ + ".mp4",
function (err) {
if (err) throw err;
}
);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment