Created
May 6, 2021 03:32
-
-
Save GustavoGomesDias/c71e12a0328893d26465c2277627b215 to your computer and use it in GitHub Desktop.
Retorna o caminho de todos arquivos de um certo tipo
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
// Retorna um caminho de um determinado tipo (node) | |
const fs = require('fs').promises; // Fazendo o fs usar promises | |
const path = require('path'); | |
async function readdir(rootDir){ | |
rootDir = rootDir || path.resolve(_dirname); | |
const files = await fs.readdir(rootDir); | |
walk(files, rootDir); | |
} | |
async function walk(files, rootDir){ | |
for(let file of files){ | |
const fileFullPath = path.resolve(rootDir, file); | |
const stats = await fs.stat(fileFullPath); | |
// Caso queira ignorar pastas como .git e a node_modules, basta descomentar as duas linhas abaixo | |
// if(/\.git/g.test(fileFullPath)) continue; | |
// if(/node_modules/g.test(fileFullPath)) continue; | |
if(stats.isDirectory()){ | |
readdir(fileFullPath); | |
continue; | |
} | |
// Basta trocar .txt pela extensão do arquivo que você quer pesquisar | |
if(/\.txt$/g.test(fileFullPath)){ | |
console.log(fileFullPath); | |
} | |
} | |
} | |
readdir('/mnt/c/Users/Demir/Desktop/Gustavo/dev/ts/node/aula4'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment