Created
June 22, 2020 21:59
-
-
Save zerobugs-oficial/5ebda2f7bf4f7386e51662c775cd052d to your computer and use it in GitHub Desktop.
Listar arquivos dentro de um diretório/subdiretórios - Node.js async/await
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
const fs = require('fs').promises; | |
async function listarArquivosDoDiretorio(diretorio, arquivos) { | |
if(!arquivos) | |
arquivos = []; | |
let listaDeArquivos = await fs.readdir(diretorio); | |
for(let k in listaDeArquivos) { | |
let stat = await fs.stat(diretorio + '/' + listaDeArquivos[k]); | |
if(stat.isDirectory()) | |
await listarArquivosDoDiretorio(diretorio + '/' + listaDeArquivos[k], arquivos); | |
else | |
arquivos.push(diretorio + '/' + listaDeArquivos[k]); | |
} | |
return arquivos; | |
} | |
async function test() { | |
let arquivos = await listarArquivosDoDiretorio('./arquivos'); // coloque o caminho do seu diretorio | |
console.log(arquivos); | |
return arquivos; | |
} | |
test(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cara, poderia utilizar isso em um array?