How do you get a list of the names of all files present in a directory in Node.js?

后端 未结 25 1169
天涯浪人
天涯浪人 2020-11-22 07:47

I\'m trying to get a list of the names of all the files present in a directory using Node.js. I want output that is an array of filenames. How can I do this?

相关标签:
25条回答
  • 2020-11-22 08:37

    Out of the box

    In case you want an object with the directory structure out-of-the-box I highly reccomend you to check directory-tree.

    Lets say you have this structure:

    photos
    │   june
    │   └── windsurf.jpg
    └── january
        ├── ski.png
        └── snowboard.jpg
    
    const dirTree = require("directory-tree");
    const tree = dirTree("/path/to/photos");
    

    Will return:

    {
      path: "photos",
      name: "photos",
      size: 600,
      type: "directory",
      children: [
        {
          path: "photos/june",
          name: "june",
          size: 400,
          type: "directory",
          children: [
            {
              path: "photos/june/windsurf.jpg",
              name: "windsurf.jpg",
              size: 400,
              type: "file",
              extension: ".jpg"
            }
          ]
        },
        {
          path: "photos/january",
          name: "january",
          size: 200,
          type: "directory",
          children: [
            {
              path: "photos/january/ski.png",
              name: "ski.png",
              size: 100,
              type: "file",
              extension: ".png"
            },
            {
              path: "photos/january/snowboard.jpg",
              name: "snowboard.jpg",
              size: 100,
              type: "file",
              extension: ".jpg"
            }
          ]
        }
      ]
    }
    

    Custom Object

    Otherwise if you want to create an directory tree object with your custom settings have a look at the following snippet. A live example is visible on this codesandbox.

    // my-script.js
    const fs = require("fs");
    const path = require("path");
    
    const isDirectory = filePath => fs.statSync(filePath).isDirectory();
    const isFile = filePath => fs.statSync(filePath).isFile();
    
    const getDirectoryDetails = filePath => {
      const dirs = fs.readdirSync(filePath);
      return {
        dirs: dirs.filter(name => isDirectory(path.join(filePath, name))),
        files: dirs.filter(name => isFile(path.join(filePath, name)))
      };
    };
    
    const getFilesRecursively = (parentPath, currentFolder) => {
      const currentFolderPath = path.join(parentPath, currentFolder);
      let currentDirectoryDetails = getDirectoryDetails(currentFolderPath);
    
      const final = {
        current_dir: currentFolder,
        dirs: currentDirectoryDetails.dirs.map(dir =>
          getFilesRecursively(currentFolderPath, dir)
        ),
        files: currentDirectoryDetails.files
      };
    
      return final;
    };
    
    const getAllFiles = relativePath => {
      const fullPath = path.join(__dirname, relativePath);
      const parentDirectoryPath = path.dirname(fullPath);
      const leafDirectory = path.basename(fullPath);
    
      const allFiles = getFilesRecursively(parentDirectoryPath, leafDirectory);
      return allFiles;
    };
    
    module.exports = { getAllFiles };
    

    Then you can simply do:

    // another-file.js 
    
    const { getAllFiles } = require("path/to/my-script");
    
    const allFiles = getAllFiles("/path/to/my-directory");
    
    0 讨论(0)
提交回复
热议问题