Skip to content

Instantly share code, notes, and snippets.

@leotm
Last active August 29, 2015 14:25
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 leotm/04d11a8a8014fdca23e0 to your computer and use it in GitHub Desktop.
Save leotm/04d11a8a8014fdca23e0 to your computer and use it in GitHub Desktop.
Node.js - Append regexp to file(s)
var glob = require('glob');
var fs = require('fs');
// Find file(s)
glob('fileName.ext', function(err, files) {
if (err) { throw err; }
files.forEach(function(item, index, array) {
console.log(item + ' found.');
// Read file(s)
var file = fs.readFileSync(item, 'utf8');
console.log('Before: ' + file);
// Append text to file(s)
fs.appendFileSync(item, 'text to append');
// Read file(s)
file = fs.readFileSync(item, 'utf8');
console.log('After: ' + file);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment