Is node.js rmdir recursive ? Will it work on non empty directories?

后端 未结 22 1353
清歌不尽
清歌不尽 2021-01-31 13:41

The documentation for fs.rmdir is very short and doesn\'t explain the behavior of rmdir when the directory is not empty.

Q: What happens if I try to use

相关标签:
22条回答
  • 2021-01-31 13:41

    Short answer: node.js fs.rmdir() calls the POSIX rmdir(); this will remove an empty directory, or return an error. In the given case, the call will invoke the callback function and pass the error as an exception.

    The problem here is that the node.js documentation refers to POSIX:

    The Node.js API Docs File System API started out as

    simple wrappers around standard POSIX functions.

    This almost changes the question into a duplicate of: Is there a listing of the POSIX API / functions?

    The description for fs.rmdir is terse, but sufficient.

    Asynchronous rmdir(2).

    The rmdir(2) here is an implicit reference to the documentation for the rmdir() system call. The number (2) here is an old unix man page convention to indicate Section 2 of the Manual pages, containing the kernel interfaces.

    0 讨论(0)
  • 2021-01-31 13:42

    tried to make it fail safe as sync removal will cause error if file or dir are in use at that time.

        var path = require('path');
    var fs = require('fs')
    
    var dumpDirs = function (dir, name, cb) {
    fs.readdir(dir, function (err, files) {
        var dirs = [],
        filePath, i = 0, l = files.length;
        for (var i = 0; i < l; i++) {
            filePath = path.join(dir, files[i]);
            var stats = fs.lstatSync(filePath);
            if (stats.isDirectory()) {
                if (files[i].indexOf(name) != -1) {
                    dirs.push({
                        startOn: new Date(stats.ctime),
                        instance: files[i],
                        name: name
                    })
                }
            }
        }
        cb(dirs);
    });
    }
    
    var removeDir = function (dir, callback) {
    fs.readdir(dir, function (err, files) {
        c = files.length;
    
        (function remfile(i, cb) {
            if (i >= c)
                return cb();
            var p = path.join(dir, files[i])
            fs.unlink(p, function (err) {
                if (err) console.log(err);
                remfile(i + 1, cb)
            });
    
        })(0, function () {
            fs.rmdir(dir, function (err) {
                callback()
            });
        });
    
        //for (var i = 0; i < c; i++) {
        //    fs.unlinkSync(path.join(dir, files[i]));
        //};
    
    
    });
    }
    dumpDirs(maindir, function (dirs) {
    
    if (dirs && dirs.length > 0) {
        (function rem(i, cb) {
            if (i >= dirs.length) {
                return cb();
            }
            var folder = path.join(dump, dirs[i].instance);
            removeDir(folder, function () {
                rem(i + 1, cb);
            });
        })(0, function () {
            callback();
        })
    }
    else {
        callback();
    }
    });
    
    0 讨论(0)
  • 2021-01-31 13:43

    fs.rmdir is not recursive.

    You could instead use a recursive fs.readdir module like readdirp in order to find all files and directories . Then remove all files, followed by all directories.

    For an even simpler solution have a look at rimraf.

    0 讨论(0)
  • 2021-01-31 13:43

    Here is the coffee script prototype function I created for fluentnode that deletes a folder recursively

    String::folder_Delete_Recursive = ->
      path = @.toString()
      if path.exists()
        for file in path.files()
          curPath = path.path_Combine(file)
          if curPath.is_Folder()
            curPath.folder_Delete_Recursive()
          else
            curPath.file_Delete()
        fs.rmdirSync(path);
    
      return path.not_Exists()
    

    here is the test:

    it 'folder_Create and folder_Delete' , ->
      tmpDir = "./".temp_Name_In_Folder()
      expect(tmpDir.folder_Exists()).to.be.false
      expect(tmpDir.folder_Create()).to.equal(tmpDir.realPath())
      expect(tmpDir.folder_Exists()).to.be.true
      expect(tmpDir.folder_Delete()).to.be.true
      expect(tmpDir.folder_Exists()).to.be.false
    
    it 'folder_Delete_Recursive' , ->
      tmpDir = "./"   .temp_Name_In_Folder().folder_Create()
      tmpFile = tmpDir.temp_Name_In_Folder().file_Create()
      expect(tmpDir.folder_Delete_Recursive()).to.be.true
    
    0 讨论(0)
  • 2021-01-31 13:43

    A neat synchronous version of rmdirSync.

    /** 
     * use with try ... catch ...
     * 
     * If you have permission to remove all file/dir
     * and no race condition and no IO exception...
     * then this should work 
     *
     * uncomment the line 
     *   if(!fs.exists(p)) return 
     * if you care the inital value of dir, 
     * 
     */
    var fs = require('fs')
    var path = require('path')
    
    function rmdirSync(dir,file){
      var p = file? path.join(dir,file):dir;
      // if(!fs.exists(p)) return 
      if(fs.lstatSync(p).isDirectory()){
        fs.readdirSync(p).forEach(rmdirSync.bind(null,p))
        fs.rmdirSync(p)
      }
      else fs.unlinkSync(p)
    }
    

    And a parallel IO, asynchronous version of rmdir. (faster)

    /**
     * NOTE: 
     * 
     * If there are no error, callback will only be called once.
     * 
     * If there are multiple errors, callback will be called 
     * exactly as many time as errors occur. 
     * 
     * Sometimes, this behavior maybe useful, but users 
     * should be aware of this and handle errors in callback. 
     * 
     */
    
    var fs = require('fs')
    var path = require('path')
    
    function rmfile(dir, file, callback){
      var p = path.join(dir, file)
      fs.lstat(p, function(err, stat){
        if(err) callback.call(null,err)
        else if(stat.isDirectory()) rmdir(p, callback)
        else fs.unlink(p, callback)
      })
    }
    
    function rmdir(dir, callback){
      fs.readdir(dir, function(err,files){
        if(err) callback.call(null,err)
        else if( files.length ){
          var i,j
          for(i=j=files.length; i--; ){
            rmfile(dir,files[i], function(err){
              if(err) callback.call(null, err)
              else if(--j === 0 ) fs.rmdir(dir,callback)
            })
          }
        }
        else fs.rmdir(dir, callback)
      })
    }
    

    Anyway, if you want a sequential IO, and callback be called exactly once (either success or with first error encountered). Replace this rmdir with the above. (slower)

    function rmdir(dir, callback){
      fs.readdir(dir, function(err,files){
        if(err) callback.call(null,err)
        else if( files.length ) rmfile(dir, files[0], function(err){
          if(err) callback.call(null,err)
          else rmdir(dir, callback)
        })
        else fs.rmdir(dir, callback)
      })
    }
    

    All of them depend ONLY on node.js and should be portable.

    0 讨论(0)
  • 2021-01-31 13:44

    I wrote about this problem exactly.

    My previous solution below, while simple, is not preferred. The following function, is a Synchronous solution; while async might be preferred.

    deleteFolderRecursive = function(path) {
        var files = [];
        if( fs.existsSync(path) ) {
            files = fs.readdirSync(path);
            files.forEach(function(file,index){
                var curPath = path + "/" + file;
                if(fs.lstatSync(curPath).isDirectory()) { // recurse
                    deleteFolderRecursive(curPath);
                } else { // delete file
                    fs.unlinkSync(curPath);
                }
            });
            fs.rmdirSync(path);
        }
    };
    

    [Edit] Added lstat instead of stat to prevent errors on symlinks

    [Previous Solution]

    My solution to this is quite easy to implement.

    var exec = require('child_process').exec,child;
    child = exec('rm -rf test',function(err,out) { 
      console.log(out); err && console.log(err); 
    });
    

    This is slimmed down for this page, but the basic idea is simple; execute 'rm -r' on the command line. If your app needs to run across different types of OS, put this in a function and have an if/else/switch to handle it.

    You will want to handle all the responses; but the idea is simple enough.

    0 讨论(0)
提交回复
热议问题