Skip to content

Instantly share code, notes, and snippets.

@bryanwb
Created May 4, 2010 08:27
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bryanwb/389141 to your computer and use it in GitHub Desktop.
/*
* 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