有一种简单的方法可以将目录中的所有文件移动到其父目录,然后删除目录吗?
我正在进行拉链提取,源zip包含一个名为的根文件夹archive
,所以当我提取时我得到了extract_path/archive/
,但我想archive
直接将内容提取到extract_path
.
我认为这将是简单的重命名,但以下是抛出"有一个文件的方式"错误消息.
fs.renameSync(extractPath + "/archive", extractPath)
Run Code Online (Sandbox Code Playgroud)
所选答案不起作用:
var mv = require('mv');
var extractPath = 'E:\\tmp\\dir';
mv(extractPath + "\\sub", extractPath, {mkdirp: true}, console.error);
Run Code Online (Sandbox Code Playgroud)
它的错误是:
{ 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' }
Run Code Online (Sandbox Code Playgroud)
使用 fs-extra 代替 mv:
var fs = require('fs-extra');
var extractPath = 'E:\\tmp\\dir';
fs.move(extractPath + "\\sub", extractPath, console.error);
Run Code Online (Sandbox Code Playgroud)
移动之前我的文件结构是这样的:
E:\tmp\dir
> sub
> doc.txt
Run Code Online (Sandbox Code Playgroud)
移动后像这样:
E:\tmp\dir
> doc.txt
Run Code Online (Sandbox Code Playgroud)
更新:
虽然上面的方法适用于 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);
});
});
};
Run Code Online (Sandbox Code Playgroud)
使用mv npm 模块。mv 首先尝试 fs.rename,如果失败,则使用 copy 然后 unlink :
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
});
Run Code Online (Sandbox Code Playgroud)
或产生一个子进程:
var spawn = require('child_process').spawn,
mv = spawn('mv', ['/dir1/dir2/*','dir1/']);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11839 次 |
最近记录: |