目录操作
创建目录
ts
import fs from "fs";
fs.mkdirSync("example/directory-1");
fs.mkdir("example/directory-2", (err) => {
if (err) throw err;
console.log("Directory created successfully");
});
读取目录
ts
import fs from "fs";
const files = fs.readdirSync("example");
console.log(files); // [ '.gitkeep', 'directory-1', 'directory-2' ]
fs.readdir("example", (err, files) => {
if (err) throw err;
console.log(files);
});
删除目录
ts
import fs from "fs";
fs.rmdirSync("example/directory-1");
fs.rmdir("example/directory-2", (err) => {
if (err) throw err;
console.log("Directory deleted successfully");
});