Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. let _getError = () => new Error(`Usage: node ${process.argv[1]} [path to folder] [unwanted name part]`);
  2.  
  3. /**
  4. * @type {String}
  5. *
  6. * Pass the folder path or return the current folder path.
  7. */
  8. let folderPath;
  9.  
  10. /**
  11. * @type {String}
  12. *
  13. * Pass the unwanted name of the folder you want to rename. If invalid will throw an error.
  14. */
  15. let unwantedFolderName;
  16.  
  17. if (3 >= process.argv.length <= 4) {
  18. if (process.argv.length === 3) {
  19. folderPath = '.';
  20. unwantedFolderName = process.argv[2];
  21. } else {
  22. folderPath = process.argv[2];
  23. unwantedFolderName = process.argv[3];
  24. }
  25. } else {
  26. throw _getError();
  27. }
  28.  
  29. const fs = require('fs');
  30.  
  31. /**
  32. *
  33. * @param path
  34. *
  35. * Return a array of folder(s) name(s).
  36. */
  37. const dirs = path => fs.readdirSync(path).filter(file => fs.statSync(path + "/" + file).isDirectory());
  38.  
  39. let foldersNames = dirs(folderPath);
  40.  
  41. if(!unwantedFolderName) {
  42. throw _getError();
  43. }
  44.  
  45. foldersNames.forEach((item) => {
  46. let oldFolder = folderPath + item
  47. , newFolder = folderPath + item.replace(unwantedFolderName, '');
  48. fs.rename(oldFolder, newFolder, (err) => {
  49. if (err) throw err;
  50. console.log(`Rename completed: "${oldFolder}" to "${newFolder}";`);
  51. });
  52. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement