Create folders synchronously
const fs = require("fs");
fs.mkdirSync("a");
Create a directory to ensure that the parent path exists
fs.mkdirSync("a/b");
Otherwise an error will be reported
The following implements a method that can be successfully created without the existence of a parent path
Synchronization method:
function mkdirSyncP(paths) {
let arr = paths.split("/");
for (let i = 0; i < arr.length; i++) {
let currentPath = arr.slice(0, i + 1).join("/");
console.log("mkdirSyncP---currentPath---->", currentPath);
// 如果文件夹不存在就创建
if (!fs.existsSync(currentPath)) {
fs.mkdirSync(currentPath);
}
}
}
mkdirSyncP("a/b/c/d/e");
Asynchronous methods: do not block the main thread
function mkdirP(paths, cb) {
let arr = paths.split("/");
let index = 0;
function next() {
// 如果路径不存在就停止创建
if (index === arr.length) return cb();
let currentPath = arr.slice(0, ++index).join("/");
// fs.exists 被废弃,可以使用 fs.access 替代
console.log("mkdirP---currentPath---->", currentPath);
fs.access(currentPath, (err) => {
// 没有文件夹就报错,报错就异步创建,不报错就 next
if (err) {
fs.mkdir(currentPath, next);
} else {
next();
}
});
}
next();
}
mkdirP("b/c/d/e/f/g", () => {
console.log("异步创建成功");
});
Asynchronous method 2: the above is used for recursion, and the following is used to for 循环 + async await
implement
const fs2 = require("fs").promises;
async function mkdirP2(paths) {
let arr = paths.split("/");
for (let i = 0; i < arr.length; i++) {
let currentPath = arr.slice(0, i + 1).join("/");
console.log("mkdirP2---currentPath---->", currentPath);
// 如果文件夹不存在就创建
try {
await fs2.access(currentPath);
} catch (error) {
console.log(error);
await fs2.mkdir(currentPath);
}
}
}
mkdirP2("c/d/e/f");