Node.js Creating and Deleting a File Recursively

Posted by Matt on Stack Overflow See other posts from Stack Overflow or by Matt
Published on 2012-03-24T03:48:35Z Indexed on 2012/03/24 5:29 UTC
Read the original article Hit count: 267

Filed under:
|

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!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about node.js