nodejs 使用经验

路径操作



 var fs = require('fs');
 var path = require('path');

    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) {

});

执行批处理命令


var exec = require('child_process').exec;

    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);
        }
    });

通过命令一次性在wps上打开多个文件

    var cmd = '"C:\\Kingsoft\\WPS Office\\ksolaunch.exe" ';

    cmd += '"';
    cmd += filePath;
    cmd += '"';

    console.log(cmd)
    exec(cmd, function(error, stdout, stderr) {
    });
posted @ 2023-04-17 09:00  lvye1221  阅读(17)  评论(0编辑  收藏  举报