Skip to content

Instantly share code, notes, and snippets.

@ahgood
Created May 28, 2016 02:27
Show Gist options
  • Save ahgood/fcb78e1a25fc20cd9dc6a0131f935365 to your computer and use it in GitHub Desktop.
Save ahgood/fcb78e1a25fc20cd9dc6a0131f935365 to your computer and use it in GitHub Desktop.
Create a file name is YYYY-MM-DD.js, if file existes, append content to file
var fs = require('fs');
var date = new Date();
var month = date.getMonth() + 1;
month = (month < 10 ? '0' : '') + month;
var day = date.getDate();
day = (day < 10 ? '0' : '') + day;
var filename = date.getFullYear() + '-' + month + '-' + day + '.js';
content = 'Content to add/append to file';
fs.stat(filename, function(err, stat) {
if (err == null) {
fs.appendFile(filename, content, function(err) {
if (err) console.error(err);
});
} else if (err.code == 'ENOENT') {
console.log('File does not exists');
fs.writeFile(filename, content, function(err) {
if(err) return console.log(err);
});
} else {
console.log('Error: ', err.code);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment