📜  NodeJS fs-extra emptyDirSync()函数

📅  最后修改于: 2022-05-13 01:56:57.356000             🧑  作者: Mango

NodeJS fs-extra emptyDirSync()函数

所述emptyDirSync()函数是emptyDir()函数的同步版本。该函数确保给定的目录为空。如果目录不为空,它将删除该目录中存在的所有内容。目录本身不会被删除。如果目录不存在,它将由函数本身创建。

句法:

fs.emptyDirSync(dir)
// OR
fs.emptydirSync(dir)

参数:此函数接受以下参数:

  • dir:它是一个包含目录路径的字符串。

返回值:该函数不返回任何内容。

请按照以下步骤实现该函数:



步骤1:可以使用以下命令安装模块:

npm install fs-extra

步骤 2:安装模块后,您可以使用以下命令检查已安装模块的版本:

npm ls fs-extra

第 3 步:创建一个名为index.js的文件,并使用以下命令在文件中引入fs-extra模块:

const fs = require('fs-extra');

第 4 步:要运行该文件,请在终端中写入以下命令:

node index.js

项目结构:项目结构如下所示。

示例 1:



文件名:index.js

Javascript
// Requiring module
const fs = require("fs-extra");
 
// Function to calculate number of files in directory
const numberOfFiles = (dir) => {
  const noOfFiles = fs.readdirSync(dir);
  return noOfFiles.length;
};
 
// Directory  path
// This directory have 2 files in it
const dir = "dir";
 
// Number of files before calling the function
const before = numberOfFiles(dir);
console.log(
  `Number of files in directory before calling the function: ${before}`
);
 
// Function call
fs.emptyDirSync(dir);
 
// Number of files after calling the function
const after = numberOfFiles(dir);
console.log(`Number of files in directory after`+
            ` calling the function: ${after}`);
console.log("Directory is empty now");


Javascript
// Requiring module
const fs = require("fs-extra");
 
// Function to calculate number of files in directory
const numberOfFiles = (dir) => {
  const noOfFiles = fs.readdirSync(dir);
  return noOfFiles.length;
};
 
// Directory path
// This directory has only 1 file
const dir = "dir/direc";
 
// Number of files before calling the function
const before = numberOfFiles(dir);
console.log(
  `Number of files in directory before calling the function: ${before}`
);
 
// Function call
fs.emptyDirSync(dir);
 
// Number of files after calling the function
const after = numberOfFiles(dir);
console.log(`Number of files in directory after`+
            ` calling the function: ${after}`);
console.log("Directory is empty now");


使用以下命令运行index.js文件:

node index.js

输出:

Number of files in directory before calling the function: 2
Number of files in directory after calling the function: 0
Directory is empty now

示例 2:

文件名:index.js

Javascript

// Requiring module
const fs = require("fs-extra");
 
// Function to calculate number of files in directory
const numberOfFiles = (dir) => {
  const noOfFiles = fs.readdirSync(dir);
  return noOfFiles.length;
};
 
// Directory path
// This directory has only 1 file
const dir = "dir/direc";
 
// Number of files before calling the function
const before = numberOfFiles(dir);
console.log(
  `Number of files in directory before calling the function: ${before}`
);
 
// Function call
fs.emptyDirSync(dir);
 
// Number of files after calling the function
const after = numberOfFiles(dir);
console.log(`Number of files in directory after`+
            ` calling the function: ${after}`);
console.log("Directory is empty now");

使用以下命令运行index.js文件:

node index.js

输出:

Number of files in directory before calling the function: 1
Number of files in directory after calling the function: 0
Directory is empty now

参考: https : //github.com/jprichardson/node-fs-extra/blob/HEAD/docs/emptyDir-sync.md