Skip to content

Instantly share code, notes, and snippets.

@GaetanoPiazzolla
Created April 21, 2021 15:44
Show Gist options
  • Select an option

  • Save GaetanoPiazzolla/b0b3278da7130c80eec2c218ff5321bb to your computer and use it in GitHub Desktop.

Select an option

Save GaetanoPiazzolla/b0b3278da7130c80eec2c218ff5321bb to your computer and use it in GitHub Desktop.
Search string in file NodeJS - FULL file in memory
const osType = require('os').type();
const {spawn} = require('child_process');
const searchOs = (filename, text) => {
return new Promise((resolve) => {
if (osType === 'Windows_NT') {
console.log('Searching using WINDOWS FIDSTR');
let child = spawn('findstr', [
'/i',
'/s',
'/c:' + text, 'file/' + filename + '.txt'],
{cwd: 'C:/workspace_private/nodejs-search-text/'});
let results = []
child.stdout.on('data', function (data) {
if (data) {
results = results.concat(data.toString().split("\r\n"))
results.pop()
}
});
child.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
child.on('close', function (code) {
console.log('child process exited with code ' + code);
resolve(results)
});
}
})
}
@GaetanoPiazzolla
Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment