使用node.js将目录中的所有文件移动到父目录 [英] Move all files in directory to parent with node.js
问题描述
是否有一种简单的方法可以将目录中的所有文件上移到其父目录然后删除该目录?
Is there a simple way to move all the files in a directory up to its parent directory then delete the directory?
我正在执行zip提取,并且源zip包含一个名为archive
的根文件夹,所以当我提取时会得到extract_path/archive/
,但是我想直接将archive
的内容提取到extract_path
.
I'm doing a zip extraction and the source zip contains a root folder called archive
, so when I extract I get extract_path/archive/
, but I'd like to just extract the contents of archive
directly to extract_path
.
我以为这将是简单的重命名,但是以下内容会引发正在路上有一个文件"错误消息.
I thought this would be simple rename, but the following is throwing a "There is a file in the way" error message.
fs.renameSync(extractPath + "/archive", extractPath)
推荐答案
使用 mv npm模块. mv首先尝试fs.rename,如果失败,则使用copy然后取消链接:
use the mv npm module. mv first tries a fs.rename, and if it fails, uses copy then 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
});
或产生一个子进程:
var spawn = require('child_process').spawn,
mv = spawn('mv', ['/dir1/dir2/*','dir1/']);
这篇关于使用node.js将目录中的所有文件移动到父目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!