Created
May 28, 2016 02:27
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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