0

I have access to a network folder where new subfolders are generated daily. In every subfolder theres one json file containing metadata. I was able to access the main folder but not the subfolders. Or at least im able to access the subfolder but only when i give the direct path to it. There starts the problem. Because every day there are new folders i'm only able to access them manually through changing the code. I tried out

    var { readdir } = require('fs/promises');

    var path1 = "somepath"

    async function main() {
        try {
            const files = await readdir(path1);
            for (const file of files)
                console.log(file);
        } catch (err) {
            console.error(err);
        }
    }
    main();

to get a list of the directories. I also tried out

    const fs = require('fs');
    const path = require('path')

    const jsonsInDir = fs.readdirSync('examplepath').filter(file => path.extname(file) === '.json');

    jsonsInDir.forEach(file => {
        const fileData = fs.readFileSync(path.join('examplepath', file));
        const json = JSON.parse(fileData.toString());
        console.log(json);
    });

To access the files but obviously it wont run through the sub directories. Maybe the answer is pretty obvious but im a newbie to this stuff.

Luis Paulo Pinto
  • 5,578
  • 4
  • 21
  • 35
VoTaNiix
  • 3
  • 2
  • How about using `glob`? https://stackoverflow.com/questions/41462606/get-all-files-recursively-in-directories-nodejs – AKX Jul 29 '21 at 10:26

1 Answers1

0

You can use a recursive function where recursively pass directory paths to process content inside it

async function processDir(dir_path) {
    let files = await readdir(dir_path);
    for (var i in files) {
      var current_object = path.join(dir_path, files[i]); //it can be file or dir
      let stat =  await fs.stat(current_object);
      if (stat.isFile()) {
        //your code to work with the file
      } else if (stat.isDirectory()) { 
       processDir(current_object); //passing dir path to process it's content
      }
    }
  };
  processDir(dir);
Vivek Bani
  • 3,703
  • 1
  • 9
  • 18