Get all files recursively in directories NodejS

前端 未结 10 1252
暖寄归人
暖寄归人 2020-12-14 07:15

I have a little problem with my function. I would like to get all files in many directories. Currently, I can retrieve the files in the file passed in parameters. I would li

相关标签:
10条回答
  • 2020-12-14 07:21
    const fs = require('fs');
    const path = require('path');
    var filesCollection = [];
    const directoriesToSkip = ['bower_components', 'node_modules', 'www', 'platforms'];
    
    function readDirectorySynchronously(directory) {
        var currentDirectorypath = path.join(__dirname + directory);
    
        var currentDirectory = fs.readdirSync(currentDirectorypath, 'utf8');
    
        currentDirectory.forEach(file => {
            var fileShouldBeSkipped = directoriesToSkip.indexOf(file) > -1;
            var pathOfCurrentItem = path.join(__dirname + directory + '/' + file);
            if (!fileShouldBeSkipped && fs.statSync(pathOfCurrentItem).isFile()) {
                filesCollection.push(pathOfCurrentItem);
            }
            else if (!fileShouldBeSkipped) {
                var directorypath = path.join(directory + '\\' + file);
                readDirectorySynchronously(directorypath);
            }
        });
    }
    
    readDirectorySynchronously('');
    

    This will fill filesCollection with all the files in the directory and its subdirectories (it's recursive). You have the option to skip some directory names in the directoriesToSkip array.

    0 讨论(0)
  • 2020-12-14 07:21

    With modern JavaScript (NodeJs 10) you can use async generator function and loop through them using for-await...of

    // ES modules syntax that is included by default in NodeJS 14.
    // For earlier versions, use `--experimental-modules` flag
    import fs from "fs/promises"
    
    // or, without ES modules, use this:
    // const fs = require('fs').promises
    
    async function run() {
      for await (const file of getFiles()) {
        console.log(file.path)
      }
    }
    
    async function* getFiles(path = `./`) {
      const entries = await fs.readdir(path, { withFileTypes: true })
    
      for (let file of entries) {
        if (file.isDirectory()) {
          yield* getFiles(`${path}${file.name}/`)
        } else {
          yield { ...file, path: path + file.name }
        }
      }
    }
    
    run()
    
    0 讨论(0)
  • You can also write your own code like below to traverse the directory as shown below :

    var fs = require('fs');
    function traverseDirectory(dirname, callback) {
      var directory = [];
      fs.readdir(dirname, function(err, list) {
        dirname = fs.realpathSync(dirname);
        if (err) {
          return callback(err);
        }
        var listlength = list.length;
        list.forEach(function(file) {
          file = dirname + '\\' + file;
          fs.stat(file, function(err, stat) {
            directory.push(file);
     if (stat && stat.isDirectory()) {
              traverseDirectory(file, function(err, parsed) {
         directory = directory.concat(parsed);
         if (!--listlength) {
           callback(null, directory);
         }
       });
     } else {
         if (!--listlength) {
           callback(null, directory);
         }
              }
          });
        });
      });
    }
    traverseDirectory(__dirname, function(err, result) {
      if (err) {
        console.log(err);
      }
      console.log(result);
    });
    

    You can check more information about it here : http://www.codingdefined.com/2014/09/how-to-navigate-through-directories-in.html

    0 讨论(0)
  • 2020-12-14 07:30

    I've seen many very long answers, and it's kinda a waste of memory space. Some also use packages like glob, but if you don't want to depend on any package, here's my solution.

    const Path = require("path");
    const FS   = require("fs");
    let Files  = [];
    
    function ThroughDirectory(Directory) {
        FS.readdirSync(Directory).forEach(File => {
            const Absolute = Path.join(Directory, File);
            if (FS.statSync(Absolute).isDirectory()) return ThroughDirectory(Absolute);
            else return Files.push(Absolute);
        });
    }
    
    ThroughDirectory("./input/directory/");
    

    It's pretty self-explanatory. There's an input directory, and it iterates through that. If one of the items is also a directory, go through that and so on. If it's a file, add the absolute path to the array.

    Hope this helped :]

    0 讨论(0)
  • 2020-12-14 07:30

    Packed into library: https://www.npmjs.com/package/node-recursive-directory

    https://github.com/vvmspace/node-recursive-directory

    List of files:

    const getFiles = require('node-recursive-directory');
    
    (async () => {
        const files = await getFiles('/home');
        console.log(files);
    })()
    

    List of files with parsed data:

    const getFiles = require('node-resursive-directory');
     
    (async () => {
        const files = await getFiles('/home', true); // add true
        console.log(files);
    })()
    

    You will get something like that:

      [
          ...,
          {
            fullpath: '/home/vvm/Downloads/images/Some/Some Image.jpg',
            filepath: '/home/vvm/Downloads/images/Some/',
            filename: 'Some Image.jpg',
            dirname: 'Some'
        },
      ]
    
    0 讨论(0)
  • 2020-12-14 07:31

    It looks like the glob npm package would help you. Here is an example of how to use it:

    File hierarchy:

    test
    ├── one.html
    └── test-nested
        └── two.html
    

    JS code:

    var getDirectories = function (src, callback) {
      glob(src + '/**/*', callback);
    };
    getDirectories('test', function (err, res) {
      if (err) {
        console.log('Error', err);
      } else {
        console.log(res);
      }
    });
    

    which displays:

    [ 'test/one.html',
      'test/test-nested',
      'test/test-nested/two.html' ]
    
    0 讨论(0)
提交回复
热议问题