Skip to content
Advertisement

Recursively read a directories with a folder

I’ve been trying to request a recursively read a directory with fs module. I had problems along the way, its only giving me a file name. Here’s how I need it to be:

  • File name.
  • And also a directory of that file. This results may be as an object or bulked into an array.

Anyone please help. Thanks.

Advertisement

Answer

Here is a recursive solution. You can test it, save it in a file, run node yourfile.js /the/path/to/traverse.

JavaScript

Output looks like this :

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement