遍历目录文件.重命名文件

var fs=require('fs');

var options={
    encoding:'utf8',
    withFileTypes:true
}

var renameFile=function(path){
    console.log('当前目录:'+path);
    fs.readdir(path,options,(err,files)=>{
        files.forEach(element => {
            if(element.isDirectory()){//目录
                var oldDirName=element.name;
                var curDirPath=path+"\\"+oldDirName;
                var newDirPath=path+"\\"+oldDirName.toLocaleLowerCase();
                fs.rename(curDirPath,newDirPath,() =>{
                    console.log('重命名 '+curDirPath+' 为小写成功!下面重命名子文件夹!');
                });
                renameFile(newDirPath);
            }else{//文件
                var oldFileName=element.name;
                var curFileFullPath=path+"\\"+oldFileName;
                var newFileFullPath=path+'\\'+oldFileName.toLocaleLowerCase();
                fs.rename(curFileFullPath,newFileFullPath,() =>{
                    console.log('重命名 '+curFileFullPath+' 为小写成功!');
                });
            }
        });
    })
}

renameFile("path");

猜你喜欢

转载自www.cnblogs.com/wolbo/p/12306623.html