• NodeJS异步、同步 创建多层文件夹


    同步:

    //创建多层文件夹 同步
    function mkdirsSync(dirpath, mode) { 
        if (!fs.existsSync(dirpath)) {
            var pathtmp;
            dirpath.split(path.sep).forEach(function(dirname) {
                if (pathtmp) {
                    pathtmp = path.join(pathtmp, dirname);
                }
                else {
                    pathtmp = dirname;
                }
                if (!fs.existsSync(pathtmp)) {
                    if (!fs.mkdirSync(pathtmp, mode)) {
                        return false;
                    }
                }
            });
        }
        return true; 
    }

    异步:

    //创建多层文件夹 异步
    function mkdirs(dirpath, mode, callback) {
        callback = callback ||
        function() {};
    
        fs.exists(dirpath,
        function(exitsmain) {
            if (!exitsmain) {
                //目录不存在
                var pathtmp;
                var pathlist = dirpath.split(path.sep);
                var pathlistlength = pathlist.length;
                var pathlistlengthseed = 0;
    
                mkdir_auto_next(mode, pathlist, pathlist.length,
                function(callresult) {
                    if (callresult) {
                        callback(true);
                    }
                    else {
                        callback(false);
                    }
                });
    
            }
            else {
                callback(true);
            }
    
        });
    }
    
    // 异步文件夹创建 递归方法
    function mkdir_auto_next(mode, pathlist, pathlistlength, callback, pathlistlengthseed, pathtmp) {
        callback = callback ||
        function() {};
        if (pathlistlength > 0) {
    
            if (!pathlistlengthseed) {
                pathlistlengthseed = 0;
            }
    
            if (pathlistlengthseed >= pathlistlength) {
                callback(true);
            }
            else {
    
                if (pathtmp) {
                    pathtmp = path.join(pathtmp, pathlist[pathlistlengthseed]);
                }
                else {
                    pathtmp = pathlist[pathlistlengthseed];
                }
    
                fs.exists(pathtmp,
                function(exists) {
                    if (!exists) {
                        fs.mkdir(pathtmp, mode,
                        function(isok) {
                            if (!isok) {
                                mkdir_auto_next(mode, pathlist, pathlistlength,
                                function(callresult) {
                                    callback(callresult);
                                },
                                pathlistlengthseed + 1, pathtmp);
                            }
                            else {
                                callback(false);
                            }
                        });
                    }
                    else {
                        mkdir_auto_next(mode, pathlist, pathlistlength,
                        function(callresult) {
                            callback(callresult);
                        },
                        pathlistlengthseed + 1, pathtmp);
                    }
                });
    
            }
    
        }
        else {
            callback(true);
        }
    
    }
  • 相关阅读:
    Ubuntu上安装RabbitMQ
    解决tfs工作区绑定问题
    ubuntu装个nginx
    supervisor执行dotnet
    Validate + Boostrap tooltip 表单验证示例
    ASP.NET下跨应用共享Session和使用Redis进行Session托管
    IE下将网页拷贝到剪贴板
    js格式化json格式的日期
    js 时间戳转换
    Windows服务简单使用
  • 原文地址:https://www.cnblogs.com/cejay/p/3557163.html
Copyright © 2020-2023  润新知