- [Instructor] You may need to move, rename,
or remove a file.
The file system module has methods for all of those as well.
So in this new file called rename.js,
we're going to get our practice in
with typing that same required statement again,
and then we're going to rename a file
with this renameSync function.
So let's check out what we want to rename.
First, we want to go to the start folder inside of lib.
We see a file called config here.
So we're going to use this
as the file that we want to rename.
So we'll provide the path to that.
Then we're going to rename this as project-config.js.
Then we will console.log("Config.js file renamed").
So let's try this.
We're going to run node rename.
It'll tell us that this was renamed,
and I can validate that by checking out the folder.
We can also use rename to move files,
and we'll show this as the asynchronous version,
so fs.rename.
Let's go ahead and add a file here.
So we'll call this notes.md inside of the lib folder.
And we'll say notes, these are my notes. Perfect.
So back to the file, we want to rename lib/notes.md.
So here we're going to move it to the correct directory.
So we want it at the root notes.md.
And then because this is the asynchronous version,
we need a function of some sort.
This function should take in an error.
If there's an error, throw it.
Otherwise, we're going to log
that the rename has been successful.
So at this point, we can run this again.
I need to comment out line three
because this file doesn't exist anymore.
We've successfully renamed it,
but if we try that one more time,
we can see that our file has been moved.
It was moved from the lib folder to the root.
The final thing I want to show here is that
files can be removed with fs unlink.
So, we want to try this here.
I'm going to comment out all of this renaming business here.
We'll comment this out too.
And we want to call fs.unlinkSync,
which will synchronously remove our project
config.js file from the folder.
So lib/project-config.js.
Let's make sure it's here, it is.
But once I run it, it's going to go away.
This lib file is now empty.
We also can asynchronously remove files
and unlink will asynchronously remove our notes file.
So here we'll say notes.md.
We'll pass in our callback function to handle an error.
If error, throw error,
otherwise we just want to console.log("Notes are gone").
Okay, so let's comment this one out 'cause that's gone.
That'll cause an error if I run it, but we see notes here.
Notes are gone.