Skip to content

Instantly share code, notes, and snippets.

@justinecodez
Last active March 28, 2019 18:56
Show Gist options
  • Save justinecodez/0b43635d729e989faa59d97c2fd5a4d8 to your computer and use it in GitHub Desktop.
Save justinecodez/0b43635d729e989faa59d97c2fd5a4d8 to your computer and use it in GitHub Desktop.
Searching for files containing a specific word in a given directory with node js
const path = require('path');
const fs = require('fs');
//dir parameter is for directory path
function searchFilesInDirectory(dir, filter, ext) {
if (!fs.existsSync(dir)) {
console.log(`Specified directory: ${dir} does not exist`);
return;
}
const files = fs.readdirSync(dir);
const found = getFilesInDirectory(dir, ext);
found.forEach(file => {
const fileContent = fs.readFileSync(file);
// We want full words, so we use full word boundary in regex.
const regex = new RegExp('\\b' + filter + '\\b');
if (regex.test(fileContent)) {
console.log(`Your word was found in file: ${file}`);
}
});
}
// Using recursion, we find every file with the desired extention, even if its deeply nested in subfolders.
function getFilesInDirectory(dir, ext) {
if (!fs.existsSync(dir)) {
console.log(`Specified directory: ${dir} does not exist`);
return;
}
let files = [];
fs.readdirSync(dir).forEach(file => {
const filePath = path.join(dir, file);
const stat = fs.lstatSync(filePath);
// If we hit a directory, apply our function to that dir. If we hit a file, add it to the array of files.
if (stat.isDirectory()) {
const nestedFiles = getFilesInDirectory(filePath, ext);
files = files.concat(nestedFiles);
} else {
if (path.extname(file) === ext) {
files.push(filePath);
}
}
});
return files;
}
console.log(searchFilesInDirectory('/home/justinelim/Desktop/node', 'justine', '.html'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment