Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Rename files in a directory with node.js

const fs = require("fs");
const path = require("path");

const folderPath = "./assets";

// read all files in the directory
let filesArr = fs.readdirSync(folderPath);

// Loop through array and rename all files 

filesArr.forEach((file, index) => {
  let fullPath = path.join(folderPath, file);
  let fileExtension = path.extname(file);
  let fileName = path.basename(file, fileExtension);

  let newFileName = fileName + index + "." + fileExtension;
try {
  fs.renameSync(fullPath, path.join(folderPath, newFileName));
} catch (error) {
  console.error(error)
}
});
Source by www.codementor.io #
 
PREVIOUS NEXT
Tagged: #Rename #files #directory
ADD COMMENT
Topic
Name
3+5 =