我试图创建一个完整的路径,如果它不存在。
代码如下所示:
var fs = require('fs');
if (!fs.existsSync(newDest)) fs.mkdirSync(newDest);
只要只有一个子目录(像'dir1'这样的newDest),这段代码就能很好地工作,但是当有一个目录路径('dir1/dir2')时,它就会失败
错误:ENOENT,没有这样的文件或目录
我希望能够用尽可能少的代码行创建完整的路径。
我读到fs上有一个递归选项,并尝试了这样做
var fs = require('fs');
if (!fs.existsSync(newDest)) fs.mkdirSync(newDest,'0777', true);
我觉得递归地创建一个不存在的目录应该这么简单。我是否遗漏了一些东西,或者我是否需要解析路径并检查每个目录,如果它不存在,则创建它?
我对Node很陌生。也许我使用的是旧版本的FS?
我对fs的递归选项有问题。mkdir,所以我做了一个函数,做以下工作:
Creates a list of all directories, starting with the final target dir and working up to the root parent.
Creates a new list of needed directories for the mkdir function to work
Makes each directory needed, including the final
function createDirectoryIfNotExistsRecursive(dirname) {
return new Promise((resolve, reject) => {
const fs = require('fs');
var slash = '/';
// backward slashes for windows
if(require('os').platform() === 'win32') {
slash = '\\';
}
// initialize directories with final directory
var directories_backwards = [dirname];
var minimize_dir = dirname;
while (minimize_dir = minimize_dir.substring(0, minimize_dir.lastIndexOf(slash))) {
directories_backwards.push(minimize_dir);
}
var directories_needed = [];
//stop on first directory found
for(const d in directories_backwards) {
if(!(fs.existsSync(directories_backwards[d]))) {
directories_needed.push(directories_backwards[d]);
} else {
break;
}
}
//no directories missing
if(!directories_needed.length) {
return resolve();
}
// make all directories in ascending order
var directories_forwards = directories_needed.reverse();
for(const d in directories_forwards) {
fs.mkdirSync(directories_forwards[d]);
}
return resolve();
});
}
这个版本在Windows上比上面的答案工作得更好,因为它同时理解/和路径。sep,以便向前斜杠在Windows上工作,因为他们应该。支持绝对路径和相对路径(相对于process.cwd)。
/**
* Creates a folder and if necessary, parent folders also. Returns true
* if any folders were created. Understands both '/' and path.sep as
* path separators. Doesn't try to create folders that already exist,
* which could cause a permissions error. Gracefully handles the race
* condition if two processes are creating a folder. Throws on error.
* @param targetDir Name of folder to create
*/
export function mkdirSyncRecursive(targetDir) {
if (!fs.existsSync(targetDir)) {
for (var i = targetDir.length-2; i >= 0; i--) {
if (targetDir.charAt(i) == '/' || targetDir.charAt(i) == path.sep) {
mkdirSyncRecursive(targetDir.slice(0, i));
break;
}
}
try {
fs.mkdirSync(targetDir);
return true;
} catch (err) {
if (err.code !== 'EEXIST') throw err;
}
}
return false;
}
您可以使用下一个函数
const recursiveUpload =(路径:字符串)=> {
Const paths = path.split("/")
const fullPath = paths.reduce((accumulator, current) => {
fs.mkdirSync(accumulator)
return `${accumulator}/${current}`
})
fs.mkdirSync(fullPath)
return fullPath
}
它的作用是:
创建路径变量,它将每个路径单独存储为数组的一个元素。
在数组中每个元素的末尾添加"/"。
形成循环:
从索引从0到当前迭代的数组元素的连接中创建一个目录。基本上,它是递归的。
希望有帮助!
顺便说一下,在Node v10.12.0中,可以通过将递归路径创建作为附加参数来使用。
fs。mkdir(“/ tmp / /苹果”,{递归:真},(err) = > {
如果(错误)抛出错误;
});
https://nodejs.org/api/fs.html#fs_fs_mkdirsync_path_options