Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7689061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:04:37+00:00 2026-05-31T20:04:37+00:00

I thought it would be a cool experiment to have a for loop and

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:04:39+00:00Added an answer on May 31, 2026 at 8:04 pm

    fs.open() using the ‘w’ param will work to create a file. As you proceed, you may encounter a second issue with your code, along the lines of this:

    /srv/node/test $ node makekill.js 
    successfully created file
    successfully created file
    successfully created file
    successfully created file
    successfully created file
    successfully created file
    successfully created file
    successfully created file
    successfully created file
    successfully created file
    successfully closed file
    
    /srv/node/test/makekill.js:13
            throw err;
            ^
    Error: ENOENT, No such file or directory './hello11.txt'
    

    With some additional logging, you’ll see that the code you’re using doesn’t actually trigger creation actions for “hello1.txt” through “hello10.txt” followed by deletion actions for the same. (Or in your sample, deletions, followed by creations, though I changed this while debugging because create/delete made more sense to me.)

    More to the point, the asynchronous callbacks seem to all use the final “i” value, rather than the value of “i” during the relevant loop.

    Long story short, I made the concept work by making a separate function which performed the desired create/delete for a given filename, and then call this function from the for loop. This seems to be a more reliable way to ensure that the intended value of “i” is used throughout the action sequence, rather than changing between the initial step and the callback step.

    var fs = require('fs');
    
    function openUnlink(name) {
      console.log(name);
      fs.open(name, 'w', function (err) {
        console.log(' + ' + name);
        fs.unlink(name, function (err) {
          console.log('   - ' + name);
        });
      });
    }
    
    for (var i=1;i<=10;i++) {
      openUnlink('hello'+i+'.txt');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a site in ASP.NET and i thought it would be cool if
I thought it would be cool to turn the hardware button lights on when
I was just thinking, and thought it would be cool if there was some
I am have started working with Silverlight and thought I would make a full
Ive always thought this would be cool, and now the OS technology seems it
I'm working on a routetracking webbapp and thought it would be cool to animate
I have always thought that replacing the normal windows desktop icons with an active
As a little side project I'd thought it would cool to make a text
I'm not sure if this is possible, but I thought it would be cool
I have a math website http://finitehelp.com that teaches students Finite Math. I thought it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.