Skip to content

Instantly share code, notes, and snippets.

@Angelfire
Created February 28, 2023 20:06
Show Gist options
  • Save Angelfire/3a6b64d94053794646ccc4d18db9510b to your computer and use it in GitHub Desktop.
Save Angelfire/3a6b64d94053794646ccc4d18db9510b to your computer and use it in GitHub Desktop.
Read Directory
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