/*同步操作,没有问题
if (fs.statSync(dirPath).isDirectory()) {
fs.readdirSync(dirPath).forEach(function(file,i){
var oldName = path.join(dirPath,file);
var newName = path.join(dirPath,"xu"+i+".jpg");
fs.renameSync(oldName,newName);
});
alert("ok");
}
*/
//等价的异步操作,一个个遍历执行重命名,每执行一次,都有部分文件被删除
fs.stat(dirPath,function (err,status) {
if(status.isDirectory()){
fs.readdir(dirPath,function(err,files){
(function next(i){
if(i<files.length){
console.log(files.length);
console.log(i);
var oldName = path.join(dirPath,files[i]);
var newName = path.join(dirPath,"xu"+i+".jpg");
fs.rename(oldName,newName,function(err){
if(err){
console.log(err);
}else{
next(++i);
}
});
}else{
alert("ok");
}
}(0));
});
}
})