使用Node.js将目录中的所有文件移动到父目录

13

问题

是否有一个简单的方法可以将目录中的所有文件移动到其父目录,然后删除该目录?

用例

我正在进行zip解压缩,源zip包含一个名为archive的根文件夹,因此当我解压缩时,我会得到extract_path/archive/,但我想直接将archive的内容提取到extract_path

我以为这很简单,只需要重命名一下即可,但是以下操作会抛出“有个文件挡住了”的错误消息。

fs.renameSync(extractPath + "/archive", extractPath)
3个回答

7

所选答案不起作用:

var mv = require('mv');
var extractPath = 'E:\\tmp\\dir';
mv(extractPath + "\\sub", extractPath, {mkdirp: true}, console.error);

它出现了错误:
{ Error: EPERM: operation not permitted, rename 'E:\tmp\dir\sub' -> 'E:\tmp\dir'
    at Error (native)
  errno: -4048,
  code: 'EPERM',
  syscall: 'rename',
  path: 'E:\\tmp\\dir\\sub',
  dest: 'E:\\tmp\\dir' }

使用fs-extra代替mv:

var fs = require('fs-extra');
var extractPath = 'E:\\tmp\\dir';
fs.move(extractPath + "\\sub", extractPath, console.error);

移动之前,我的文件结构是这样的:

E:\tmp\dir
    > sub
        > doc.txt

移动后,就像这样:
E:\tmp\dir
    > doc.txt

更新:

虽然上述方法在Windows上可以使用,在Linux上即使使用fs-extra也会出现相同的错误。下面是一种手动修复方法,通过逐个将子目录的每个子项移动到父目录中来解决这个问题。如果某个子项移动失败,则会将其他成功移动的子项恢复到原始位置。

var fs = require('fs-extra')
var Promise = require('promise');
var path = require('path');


var promiseAllWait = function(promises) {
    // this is the same as Promise.all(), except that it will wait for all promises to fulfill before rejecting
    var all_promises = [];
    for(var i_promise=0; i_promise < promises.length; i_promise++) {
        all_promises.push(
            promises[i_promise]
            .then(function(res) {
                return { res: res };
            }).catch(function(err) {
                return { err: err };
            })
        );
    }

    return Promise.all(all_promises)
    .then(function(results) {
        return new Promise(function(resolve, reject) {
            var is_failure = false;
            var i_result;
            for(i_result=0; i_result < results.length; i_result++) {
                if (results[i_result].err) {
                    is_failure = true;
                    break;
                } else {
                    results[i_result] = results[i_result].res;
                }
            }

            if (is_failure) {
                reject( results[i_result].err );
            } else {
                resolve(results);
            }
        });
    });
};

var movePromiser = function(from, to, records) {
    return fs.move(from, to)
    .then(function() {
        records.push( {from: from, to: to} );
    });
};

var moveDir = function(from_dir, to_dir) {
    return fs.readdir(from_dir)
    .then(function(children) {
        return fs.ensureDir(to_dir)
        .then(function() {
            var move_promises = [];
            var moved_records = [];
            var child;
            for(var i_child=0; i_child < children.length; i_child++) {
                child = children[i_child];
                move_promises.push(movePromiser(
                    path.join(from_dir, child),
                    path.join(to_dir, child),
                    moved_records
                ));
            }

            return promiseAllWait(move_promises)
            .catch(function(err) {
                var undo_move_promises = [];
                for(var i_moved_record=0; i_moved_record < moved_records.length; i_moved_record++) {
                    undo_move_promises.push( fs.move(moved_records[i_moved_record].to, moved_records[i_moved_record].from) );
                }

                return promiseAllWait(undo_move_promises)
                .then(function() {
                    throw err;
                });
            });
        }).then(function() {
            return fs.rmdir(from_dir);
        });
    });
};

5
使用mv npm模块。mv首先尝试fs.rename,如果失败,则使用复制再删除的方式:
mv('source/dir', 'dest/a/b/c/dir', {mkdirp: true}, function(err) {
  // done. it first created all the necessary directories, and then
  // tried fs.rename, then falls back to using ncp to copy the dir
  // to dest and then rimraf to remove the source dir
});

或者生成一个子进程:

var spawn = require('child_process').spawn,
    mv = spawn('mv', ['/dir1/dir2/*','dir1/']);

你知道这个有没有同步版本吗? - AlexZ
使用 execSync:var spawn = require('child_precess').execSync; - chourobin
1
这个答案无法将子目录的内容移动到父目录(问题所在)。运行 mv('E:\\tmp\\dir\\sub', 'E:\\tmp\\dir', {mkdirp: true}, console.error); 会失败并显示 Error: EPERM: operation not permitted, rename 'E:\tmp\dir\sub' -> 'E:\tmp\dir' - sam-6174
当目标文件夹非空时无法工作。【错误:ENOTEMPTY:目录非空】 - anonymous

2

对我来说,这些答案都没用,我深入研究了mv的代码并找到了解决方案:

我将 folder/subfolder 移动到 folder,所以该文件夹已经存在。

mv(oldPath, newPath, {mkdirp: false, clobber: false}, (err) => {
    if (err) {
        throw err;
    }
});

记住,如果父文件夹中已经存在相同名称的文件,则会被子文件夹中的文件覆盖。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接