简体   繁体   English

Node.js递归创建和删除文件

[英]Node.js Creating and Deleting a File Recursively

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. 我认为有一个for循环并创建一个文件hello.txt然后用unlink删除它将是一个很酷的实验。 I figured that if fs.unlink is the delete file procedure in Node, then fs.link must be the create file. 我想如果fs.unlink是Node中的删除文件过程,那么fs.link必须是创建文件。 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. 即使我将fs.link代码分成单独的文件,它仍然不会创建我的文件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 http://nodejs.org/api/fs.html#fs_fs_link_srcpath_dstpath_callback

Thank you! 谢谢!

fs.link creates a symlink (that is it makes a "link file" that points at another file) fs.link创建一个符号链接(即它创建一个指向另一个文件的“链接文件”)

You want fs.writeFile 你想要fs.writeFile

fs.open() using the 'w' param will work to create a file. 使用'w'参数的fs.open()将用于创建文件。 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. 通过一些额外的日志记录,您将看到您正在使用的代码实际上并未通过“hello10.txt”触发“hello1.txt”的创建操作,然后执行相同的删除操作。 (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. 更重要的是,异步回调似乎都使用最终的“i”值,而不是相关循环期间的“i”值。

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. 简而言之,我通过创建一个单独的函数来使概念工作,该函数对给定的文件名执行所需的创建/删除,然后从for循环调用此函数。 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. 这似乎是一种更可靠的方法,可确保在整个动作序列中使用“i”的预期值,而不是在初始步骤和回调步骤之间进行更改。

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');
}

The answers are correct, just commenting on why. 答案是正确的,只是评论为什么。 In Unix (and it's the Unix syscall API being approximated here), unlink is used to remove a hard link to a file. 在Unix中(这里是Unix系统调用API),unlink用于删除文件的链接。 When no further hard links remain the file is considered deleted. 当没有其他硬链接时,该文件被视为已删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM