Skip to content

Instantly share code, notes, and snippets.

@bryanwb
Created May 4, 2010 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryanwb/389396 to your computer and use it in GitHub Desktop.
Save bryanwb/389396 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 = name.replace(/[0-9]+$/, function (num) {
return parseInt(num) + 10;
});
sys.puts(newName);
return newName;
};
function renameGroup(group, callback){
group.forEach(
function(file){
sys.puts(file);
Step(
function(){
fs.stat(file, this);
},
function(err, stat) {
sys.puts(stat.ino);
fs.rename(file, newName(file), this);
},
callback()
);
}
);
};
Step(
function (){
renameGroup(group1, this.group());
},
function (){
renameGroup(group2, this.group());
},
function (){
renameGroup(group3, this.group());
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment