Skip to content

Instantly share code, notes, and snippets.

@sistematico
Forked from uelsson/files.js
Last active March 21, 2023 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sistematico/29afbd749079c0f316edac503d37d024 to your computer and use it in GitHub Desktop.
Save sistematico/29afbd749079c0f316edac503d37d024 to your computer and use it in GitHub Desktop.
Node.js - Como listar arquivos com fs, path
// 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