- [Instructor] In the last lesson,
we used the file system modules rename method
to rename and move files.
So you can use that same method
to rename and move directories as well.
The first thing we'll do inside of this directory's file
is we're going to say fs require our file system module.
And the method we want to use from this is renameSync.
We can use this to rename a directory.
So we have some here for you.
We have a folder called assets, logs,
and we want to go ahead and move it to the accounts folder.
We'll say assets/logs
and accounts/logs is where we want to move this.
Now, if we console log logs folder moved
and we run node directories,
this is going to say
that this was moved and we see that this is here.
Now, something to note too,
is that this already had a file in it.
So we can see that the file directory has been moved
with all of the files in it.
We also saw in the last video,
how we've removed files with the unlink method,
but to remove directories we'll need to use a different one.
So let's go back to our directories file.
We're going to comment out our renameSync
as well as our console log for now.
And we are going to use fs.rmdir
and we want to delete the accounts folder.
So here we're going to handle errors as you might expect
console.log our error,
and then otherwise we want to let our users know
that we're finished.
So we'll say console.log accounts directory removed.
Cool, so we see it's here.
If we run this again,
we'll see that this folder is not empty.
So we're getting an error about that.
So let's try to replace this.
We'll say assets, 'cause assets is empty, right?
We moved the files from that.
So we'll try that again.
And it'll say accounts directory removed
you know what we meant, assets directory removed,
and that has been removed.
So again, the removed directory
and removed directory sync methods,
aren't going to work if there are files or sub directories
in that directory.
So if you want to remove the directory,
we have to get rid of everything that's inside of it.
So here's what we'll do.
First things first,
I want to comment out this first one
we already got rid of that assets folder.
Here we'll say fs.readdireSync.
And we want to read from our accounts folder.
And what we'll do here,
is we want to iterate over this with forEach.
So this is going to return an array of all the file names
and the sub directory names directly here.
We'll chain on forEach and loop through every file name
that's in that array.
So for each file,
we're going to do the following fs.renameSync.
For each file, we want to move it to the new directory.
We also need to fix that little arrow function there.
Then let's console.log files removed
and then we'll remove synchronously our accounts directory.
After we're done with that,
let's go ahead and console.log
that the folder has been removed.
So if we run this again with no directories,
we're still going to get that error.
So let's create a new folder.
We'll call it library.
We should see that the files are removed,
the folder is removed,
and everything has been moved there accordingly.
So our file CLI is getting pretty powerful.
Now we have the ability to remove files and directories.