Question
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 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)
The selected answer does not work:
It errors with:
Use fs-extra instead of mv:
My file structure is like this before the move:
And like this after the move:
UPDATE:
While the above works on Windows, on Linux I get the same error even when using fs-extra. The below is a manual fix for this, by individually moving each child of the subdirectory up to the parent. If a child move fails, then it will revert any other successful moves back to the original location in the subdirectory.
use the mv npm module. mv first tries a fs.rename, and if it fails, uses copy then unlink :
or spawn a child process :