Created
February 28, 2023 20:06
-
-
Save Angelfire/3a6b64d94053794646ccc4d18db9510b to your computer and use it in GitHub Desktop.
Read Directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs from "fs"; | |
function readDirectory(path, depth = 0) { | |
const indent = " ".repeat(depth * 2); | |
const files = fs.readdirSync(path); | |
for (const file of files) { | |
const filePath = `${path}/${file}`; | |
const stats = fs.statSync(filePath); | |
if (stats.isDirectory()) { | |
console.log(`${indent}- ${file}/`); | |
readDirectory(filePath, depth + 1); | |
} else { | |
console.log(`${indent}- ${file}`); | |
} | |
} | |
} | |
readDirectory("."); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment