Here is a solution using Node.js and the built-in File System (fs) module:
index.tsx631 chars26 lines
This function takes a directory path as an argument and uses fs.readdirSync
to get a list of all files in the directory. It then loops through each file and checks if it is a directory using fs.statSync
. If it is a directory, it recursively calls the function with the subdirectory path. If it is a file, it prints the full path to the console using console.log
and path.join
.
Note that fs.readdirSync
is a synchronous function and will block the event loop until it completes, so this solution may not be suitable for very large directories. In that case, you can use the asynchronous version fs.readdir
and refactor the function to use promises or callbacks.
gistlibby LogSnag