Guest User

Untitled

a guest
May 24th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. /*
  2. * This demo program tries to rename 3 groups of files without overwriting any of the files in any other groups
  3. * The files are numbered quasi-consecutively tmp1, tmp2, tmp3, tmp11, tmp12, tmp13, etc.
  4. * I rename the files by adding 10 to the numeric part of the name
  5. * tmp1 cannot be renamed to tmp11 until the original tmp11 has been renamed to tmp21
  6. *
  7. */
  8. var Step = require('step');
  9. var fs = require('fs');
  10. var sys = require('sys');
  11.  
  12. var group1 = ["/tmp/tmp21", "/tmp/tmp22", "/tmp/tmp23"];
  13. var group2 = ["/tmp/tmp11", "/tmp/tmp12", "/tmp/tmp13"];
  14. var group3 = ["/tmp/tmp1", "/tmp/tmp2", "/tmp/tmp3"];
  15.  
  16.  
  17. function newName(name){
  18. var newName = "";
  19. var num = parseInt(name.slice(-2));
  20. if(isNaN(num)){
  21. num = parseInt(name.slice(-1));
  22. num = num + 10;
  23. newName = name.slice(0, -1) + num;
  24. } else {
  25. num = num + 10;
  26. newName = name.slice(0, -2) + num;
  27. }
  28.  
  29. sys.puts(newName);
  30. return newName;
  31. };
  32.  
  33. function renameGroup(groupNames, group){
  34. groupNames.forEach(
  35. function(file){
  36. sys.puts(file);
  37. fs.rename(file, newName(file), group());
  38. }
  39. );
  40. };
  41.  
  42. //this actually does work but as you can see there is lots of boilerplate
  43. function renameFiles(){
  44. Step(
  45. function (){
  46. var group = this.group();
  47. renameGroup(group1, group);
  48. },
  49. function (){
  50. var group = this.group();
  51. renameGroup(group2, group);
  52. },
  53. function (){
  54. var group = this.group();
  55. renameGroup(group3, group);
  56. }
  57. );
  58. };
  59.  
  60. renameFiles();
Add Comment
Please, Sign In to add comment