This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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; | |
}; | |
//this actually does work but as you can see there is lots of boilerplate | |
function renameFiles(){ | |
Step( | |
function (){ | |
var parallel = this.parallel; | |
group1.forEach( | |
function(file){ | |
sys.puts(file); | |
fs.rename(file, newName(file), parallel()); | |
}); | |
}, | |
function (){ | |
var parallel = this.parallel; | |
group2.forEach( | |
function(file){ | |
sys.puts(file); | |
fs.rename(file, newName(file), parallel()); | |
}); | |
}, | |
function (){ | |
var parallel = this.parallel; | |
group3.forEach( | |
function(file){ | |
sys.puts(file); | |
fs.rename(file, newName(file), parallel()); | |
}); | |
} | |
); | |
}; | |
//renameFiles(); | |
renameFiles(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment