Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This demo program tries to rename 3 groups of files without overwriting any of the files in any other groups
- * The files are numbered quasi-consecutively tmp1, tmp2, tmp3, tmp11, tmp12, tmp13, etc.
- * I rename the files by adding 10 to the numeric part of the name
- * tmp1 cannot be renamed to tmp11 until the original tmp11 has been renamed to tmp21
- *
- */
- var Step = require('step');
- var fs = require('fs');
- var sys = require('sys');
- var group1 = ["/tmp/tmp21", "/tmp/tmp22", "/tmp/tmp23"];
- var group2 = ["/tmp/tmp11", "/tmp/tmp12", "/tmp/tmp13"];
- var group3 = ["/tmp/tmp1", "/tmp/tmp2", "/tmp/tmp3"];
- function newName(name){
- var newName = "";
- var num = parseInt(name.slice(-2));
- if(isNaN(num)){
- num = parseInt(name.slice(-1));
- num = num + 10;
- newName = name.slice(0, -1) + num;
- } else {
- num = num + 10;
- newName = name.slice(0, -2) + num;
- }
- sys.puts(newName);
- return newName;
- };
- function renameGroup(groupNames, group){
- groupNames.forEach(
- function(file){
- sys.puts(file);
- fs.rename(file, newName(file), group());
- }
- );
- };
- //this actually does work but as you can see there is lots of boilerplate
- function renameFiles(){
- Step(
- function (){
- var group = this.group();
- renameGroup(group1, group);
- },
- function (){
- var group = this.group();
- renameGroup(group2, group);
- },
- function (){
- var group = this.group();
- renameGroup(group3, group);
- }
- );
- };
- renameFiles();
Add Comment
Please, Sign In to add comment