Ralph Callaway Ralph Callaway - 4 months ago 33x
Node.js Question

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?

Use Case

I'm doing a zip extraction and the source zip contains a root folder called
, so when I extract I get
, but I'd like to just extract the contents of
directly to

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)


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

or spawn a child process :

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