Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  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. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement