路径操作
var dirName = path.dirname(filePath);
var fileName = path.basename(filePath);
var newFilePath = dirName + "/min-" + fileName;
function walkSync(currentDirPath, callback) {
fs.readdirSync(currentDirPath, { withFileTypes: true }).forEach(function(dirent) {
var filePath = path.join(currentDirPath, dirent.name);
if (dirent.isFile()) {
callback(filePath, dirent);
} else if (dirent.isDirectory()) {
walkSync(filePath, callback);
}
});
}
// 遍历所有文件
walkSync(resDir, function(filePath, stat) {
});
执行批处理命令
let cmd = obj.cmd;
let oldFilePath = obj.oldFilePath;
exec(cmd, function(error, stdout, stderr) {
if(error){
console.error(error);
} else {
var msg = cmd + " success!";
console.log(msg);
// 删除文件
fs.unlinkSync(oldFilePath);
}
});
标签:function,经验,filePath,nodejs,cmd,error,使用,var,dirent
From: https://www.cnblogs.com/lvye1221/p/17324716.html