SHARE
TWEET

Untitled

a guest Mar 22nd, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require("fs");
  2. const path = "./"; // current folder
  3.  
  4. fs.readdir(path, function(err, items) {
  5.   console.log('Old filenames:');
  6.   console.log(items);
  7.  
  8.   for (var i = 0; i < items.length; i++) {
  9.  
  10.     const file = items[i];
  11.  
  12.     // only apply to jpg's in the folder
  13.     if (file.substr(-3) === 'jpg'){
  14.       // adjust this line to
  15.       let newFilename = file.substring(file.indexOf('_', file.indexOf('_') + 1) + 1);
  16.      
  17.       console.log(`Renaming ${file} to ${newFilename}`);
  18.  
  19.       fs.rename(file,newFilename, (err) => {
  20.         if (err) throw err;
  21.       });
  22.  
  23.       console.log(newFilename);
  24.     }
  25.   }
  26.   console.log('Finished');
  27.  
  28. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top