fs.Dir & fs.Dirent

Dir is a class for an iterable directory stream, and fs.dirent is a directory entry obtained by traversing fs.dir, which can be a file or subdirectory within a directory

fs.Dir

  • Dir. path: indicates the read-only path of a directory
  • Dir.read () : Returns a Promise without passing callabck, reads the next directory entry in the iterator, returns a Promise, resolves to fs.dirent or null (if there are no more directory entries to read)
  • Dir.close () : Returns a Promise if no callabck is passed, closing the underlying resource handle to the directory

fs.Dirent

  • dirent.name
  • dirent.isDirectory()
  • dirent.isFile()
  • dirent.isSymbolicLink()

fs.opendir

Fs.opendir (path[, options], callback) opens a directory and returns the fs.dir object

const fs = require('fs/promises');

async function print(path) {
  const dir = await fs.opendir(path);
  for await (const dirent of dir) {
    console.log(dirent.name);
  }
}
print('/').catch(console.error);
Copy the code

Dir can be iterated through dir.read()

const fs = require('fs/promises');

async function print(path) {
  const dir = await fs.opendir(path);
  let dirent = await dir.read();
  while (dirent) {
    console.log(dirent.name);
    dirent = await dir.read();
  }

  dir.close();
}
print('/').catch(console.error);
Copy the code

fs.readdir

Fs.readdir (path[, options], callback) reads the contents of the directory, and the callback takes two arguments (err, files), where files is an array of file names in the directory (excluding ‘.’ and ‘.. ‘) options

  • Encoding: The default value is UTf8. If encoding is set to ‘buffer’, the file name returned is the buffer object
  • WithFileTypes: Default false, set to true and the callback function Files array will contain the fs.dirent object
const fs = require('fs/promises');

async function print(path) {
  const files = await fs.readdir(path);
  for (const file of files) {
    console.log(file);
  }
}
print('/').catch(console.error);
Copy the code

fs.mkdir

Fs.mkdir (path[, options], callback) create directory options

  • Recursive: Default false, equivalent to command when set to truemkdir -pCreates a directory that does not exist
  • Mode: The default value is 0o777, which is not supported by Windows
// Create the/TMP /a/apple directory, regardless of whether/TMP and/TMP /a exist.
fs.mkdir('/tmp/a/apple', { recursive: true }, err= > {
  if (err) throw err;
});
Copy the code

fs.rmdir

Fs.rmdir (path[, options], callback) fs.rmdir is used to delete folder options

  • Recursive: Default false, if true, recursive directory deletion is performed. In recursive mode, no errors are reported if path does not exist, and the operation is retried if it fails
  • RetryDelay: The default value is 100. The number of milliseconds to wait between retries after an exception occurs. If the recursive option is not true, this option is ignored
  • MaxRetries: The default value is 0, which indicates the number of retries after an exception occurs. If an EBUSY, EMFILE, ENFILE, ENOTEMPTY, or EPERM error is encountered, Node.js will retry the operation with a linear retryDelay of milliseconds on each attempt. If recursive is false, this option is ignored
const fs = require('fs');

fs.rmdir('./tmp', { recursive: true }, err= > console.log);
Copy the code

Previously rmdir could only delete empty folders, but now it can delete them along with files

The minimalist Node. Js introductory tutorial: www.yuque.com/sunluyong/n…

In this paper, a better reading experience: www.yuque.com/sunluyong/n…