node 写入、创建、删除

28 阅读1分钟
import path from "node:path";
import fsPromise from "node:fs/promises";

//写入
const filePath = path.resolve(import.meta.dirname, "./text/text.js");
fsPromise
  .writeFile(filePath, "hello-word" + new Date().toDateString())
  .then(() => {
    console.log("写入成功");
  });

async function test() {
  const uid = (Math.random() * 10).toString(32);
  const FilePath = path.resolve(import.meta.dirname, `./text/${uid}text.js`);
  //创建
  await fsPromise.writeFile(FilePath, "hello-word");
  //查看状态
  await fsPromise.stat(FilePath);
  //删除
//   DeleteAllFiles();
}


async function DeleteAllFiles() {
  const filePath = path.resolve(import.meta.dirname, "./text");
  const fileLists = await fsPromise.readdir(filePath);
  fileLists.map((item) => {
    fsPromise.unlink(path.resolve(filePath, item));
  });
}
test()