-
-
Save sistematico/29afbd749079c0f316edac503d37d024 to your computer and use it in GitHub Desktop.
Node.js - Como listar arquivos com fs, path
This file contains hidden or 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
// Original em: https://youtu.be/Eeqyi6W4CqY | |
import fs from 'fs' | |
import path from 'path' | |
let data = [] | |
function listarArquivos(dir) { | |
const directories = fs.readdirSync(dir) | |
for (const file in directories) { | |
const filePath = path.join(dir, directories[file]) | |
const stats = fs.lstatSync(filePath) | |
if (stats.isDirectory() || stats.isSymbolicLink()) { | |
try { | |
listarArquivos(filePath) | |
} catch(err) { | |
continue | |
} | |
} else { | |
data.push({ | |
file: path.basename(filePath), | |
path: filePath, | |
size: stats.size, | |
type: path.extname(filePath) | |
}) | |
} | |
} | |
} | |
listarArquivos('../example') | |
console.log(JSON.stringify({ data }, null, 2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment