Node.js Creating and Deleting a File Recursively
- by Matt
I thought it would be a cool experiment to have a for loop and create a file hello.txt and then delete it with unlink. I figured that if fs.unlink is the delete file procedure in Node, then fs.link must be the create file. However, my code will only delete, and it will not create, not even once. Even if I separate the fs.link code into a separate file, it still will not create my file hello.txt.
Below is my code:
var fs = require('fs'),
for(var i=1;i<=10;i++){
fs.unlink('./hello.txt', function (err) {
if (err){
throw err;
} else {
console.log('successfully deleted file');
}
fs.link('./hello.txt', function (err) {
if (err){
throw err;
} else {
console.log('successfully created file');
}
});
});
}
http://nodejs.org/api/fs.html#fs_fs_link_srcpath_dstpath_callback
Thank you!