Skip to content

Instantly share code, notes, and snippets.

@crubier
Created January 17, 2017 10:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save crubier/3cfcd4931f9c52d93fed4c18d2edee16 to your computer and use it in GitHub Desktop.
Save crubier/3cfcd4931f9c52d93fed4c18d2edee16 to your computer and use it in GitHub Desktop.
Apply Regex to all files in directory, using node js
var fs = require('fs');
function readFiles(dirname, onFileContent, onError) {
fs.readdir(dirname, function(err, filenames) {
if (err) {
onError(err);
return;
}
filenames.forEach(function(filename) {
fs.readFile(dirname + filename, 'utf-8', function(err, content) {
if (err) {
onError(err);
return;
}
onFileContent(filename, content);
});
});
});
}
readFiles('./', function(filename, content) {
console.log(filename);
// var newcontent=content.replace(/\n"\nauthor/,"\nauthor");
var newcontent=content.replace(/title:\s*"([^"]*)\nauthor/,"title: \"$1\"\nauthor");
fs.writeFile(filename,newcontent,{encoding:'utf8'},
function(){
console.log('OK '+filename);
});
}, function(err) {
throw err;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment