Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
* 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(group){
group.forEach(
function(file){
sys.puts(file);
rename(file);
}
);
};
function idealRenameFiles(){
Step(
function renameGroup1 (){
renameGroup(group1, this);
},
function renameGroup2 (){
renameGroup(group2, this);
},
function renameGroup3(){
renameGroup(group3);
}
);
sys.puts("success");
};
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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment