Looping through files in a folder Node.JS

后端 未结 3 1049
耶瑟儿~
耶瑟儿~ 2020-12-07 18:31

I am trying to loop through and pick up files in a directory, but I have some trouble implementing it. How to pull in multiple files and then move them to another folder?

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-07 18:53

    fs.readdir(path[, options], callback) (which Mikey A. Leonetti used in his answer) and its variants (fsPromises.readdir(path[, options]) and fs.readdirSync(path[, options])) each reads all of a directory's entries into memory at once. That's good for most cases, but if the directory has very many entries and/or you want to lower your application's memory footprint, you could instead iterate over the directory's entries one at a time.

    Asynchronously

    Directories are async iterable, so you could do something like this:

    const fs = require('fs')
    
    async function ls(path) {
      const dir = await fs.promises.opendir(path)
      for await (const dirent of dir) {
        console.log(dirent.name)
      }
    }
    
    ls('.').catch(console.error)
    

    Or, you could use dir.read() and/or dir.read(callback) directly.

    Synchronously

    Directories aren't sync iterable, but you could use dir.readSync() directly. For example:

    const fs = require('fs')
    
    const dir = fs.opendirSync('.')
    let dirent
    while ((dirent = dir.readSync()) !== null) {
      console.log(dirent.name)
    }
    dir.closeSync()
    

    Or, you could make directories sync iterable. For example:

    const fs = require('fs')
    
    function makeDirectoriesSyncIterable() {
      const p = fs.Dir.prototype
      if (p.hasOwnProperty(Symbol.iterator)) { return }
      const entriesSync = function* () {
        try {
          let dirent
          while ((dirent = this.readSync()) !== null) { yield dirent }
        } finally { this.closeSync() }
      }
      if (!p.hasOwnProperty(entriesSync)) { p.entriesSync = entriesSync }
      Object.defineProperty(p, Symbol.iterator, {
        configurable: true,
        enumerable: false,
        value: entriesSync,
        writable: true
      })
    }
    makeDirectoriesSyncIterable()
    

    And then, you could do something like this:

    const dir = fs.opendirSync('.')
    for (const dirent of dir) {
      console.log(dirent.name)
    }
    

    Note: "In busy processes, use the asynchronous versions of these calls. The synchronous versions will block the entire process until they complete, halting all connections."

    References:

    • Node.js Documentation: File System: Class fs.Dir
    • Node.js source code: fs.Dir
    • GitHub: nodejs/node: Issues: streaming / iterative fs.readdir #583

提交回复
热议问题