using mkdir() in copyThenRemove() leads to errors if the target exists or not a dir, use ensureDirectoryExists() instead