Node.js FS Module: Complete Guide to CRUD Operations & File Renaming (Sync)

Thapa Technical
63 followers
Nov 29, 2024
Hello what's up guys today in this video
we will see FS module in our nut j
application yes guys today's video is going to be very
awesome and if you have
n't checked out the previous videos then
definitely check them out guys today we Finally we are going to
see the FS module which
is called your file system module and this is a
very important model because finally
now how can you read the content of any file in your system.
How can you update its data or
any file,
how can you delete it, how
can you change the name of files and folders,
how can you add data to it or
update it. You can append, you can do
all this, basically
this module is going to give you the fun of playing with the file system
and this is very
important. Guys, it is very important in believe,
so please read it. You will
definitely watch till the end because
it is very useful in big projects, so
first of all let me tell you, although what I
told, it is written here also, it is a
file system module in the nut, which
comes inside the core module. That allows you
to work with the file system enabling you to
read write update delete and watch the files and
we today in this video I am
going to tell you the complete code operation,
create the files and also read them.
I will also update and then delete it and show it,
then all your doubts will be cleared, so
guys, without any delay, now let's
start the video, but before that, thank you
so much, I am watching the comments, very good
comments, day four present sir. Day Five
Present Sir Day Six Present Sir By
doing this today I don't know what day it is but
whatever it is guys I feel very happy and
please keep your love like this End
Channel Don't forget to subscribe.
Okay, so I'll go to my VS Code
editor. You can see that I have already given the
definition of how we can do this right here. I thought I would ask you to give
each and every thing definition with syntax. A
further definition of what the
particular arguments we are passing in that syntax
means so that you are
left with no room for doubt. That's it,
make a video, but if there is no one, then
look at the guys in the curd operation, CRUD, okay,
so we will perform one thing, just yes,
keep one thing in mind here, now we will start
one thing, this is what is
in the file system here. One is your
synchronous method and the other is your
asynchronous method which we do through call back
but in JavaScript you have
already learned that like call back hell is also a
topic due to which we call like promise. To
simplify the write and then promise further,
we used like a sync await hai na to
yahan pe bhi kuch vo funda hai to every day we will perform the
curd operation but in a different way, today
I will tell you using A Synchronous
Way Tomorrow we will see again curd operation but
using an asynchronous way which is best
non blocking but with the help of call
back then we will see again your Card operation
but with the help of promise is a different
world then finally I will tell you
how you can do card operation with the help of promise in your FS module so
these four videos are very important you should not
miss them at all You can't do it literally,
all the videos from here are very important
and today in this I will give you
some glimpses of the previous models that we read
and I will ask you questions. We do
n't have to sit, we have to
practice also, okay, so
see, first of all we have to create a file,
okay, now you can see very clearly,
look carefully once, if you feel it carefully then
pause, we have this FS
folder, currently only There is a file and there are
so many files here, okay, I would
like to create a new file here,
whose name will be FS Sync, meaning
we are doing FS in a synchronous manner, okay and
again one more time is not here.
Here There is no OK sync FS is there but
FS sync is not there I do one thing na
text text speaking test data one
thing I create the file okay so
see carefully now how we can do so
FS module basically with this module
To play we need to go with the fs.
Now I won't get any suggestion here
Antya so request it right like we
were doing with path, if we were doing with dew then
const fs. Will be we need to request
the FS or this is it so okay this is my
work now here I will go and then
press dot then boom guys see you will
see multiple suggestions here you
can see very clearly that one you It is visible that the
read file is sync, right, see one thing here, look
carefully, this is your synchronous
because here there is sync and same
perform I mean to perform this operation
we have one. More method called read
file which is your asynchronous method okay
so right now what we are looking at is the
synchronous method so FS dot we have a
write we have a file and then sync now here
you can see the first file in the argument
Path the file path to write to
means now here you have to give the path of the file
where you want to add this data.
Now there are some things here too,
this is the path of the file. If that file does
not exist then it automatically
creates a new file then it
basically adds the include to the data you pass
and then we have this option
basically the format of coding is like
mostly this is If you
want a very deep video, then click on this particular
option only because a very
big video can be made on this, even you can see the image,
we have 8 likes. The base is 24, it
is ASCII, it is Haxe, it is full, but there is
none, it is just one encoding method, I hope
you understand, so let me do one thing,
I would like to create a file with lets,
const a file, I will give the name of it.
Test here txt.gz
This is the initial ok, initial is
not commit, I am writing a comment, but let's go, there is
no git hup, there is a little end comma
here, UT is an encoded format, ok.
Which is also very important, one second I
guess, I pressed insert,
everything is fine here, UTF is not a problem now
and don't worry, it has a full form which I would
like to keep, it is 8 bit Unicode
transformation format and codes
characters from near. All written languages,
symbols and then emojis are fine, I will do one thing,
I will put it above and comment,
right now I don't need it that much,
but or Whatever you understand, now
I add it at one place, basically
stores right file, it is okay, it was not a big deal,
I have to create a file or
I have to add some data in a file, then
what is the name of the file, basically the path? What data do I have to add
and in which format do I have to add it?
This is it, so let's go with Kasol
Dlog and let's go with write file. Okay,
perfect, okay, I go here,
clear everything. First, here I would
like to share one thing,
test data is not visible anywhere here,
okay, that's good, now I
go here, not within a second advance,
it's a note, first, what is here? Ok CD
node Thapa, there is
something inside it, there are many of them, so what are we
inside, we are inside FS, so we
have FS, okay, there is something inside it, okay,
this is basically, I have to run this file,
okay, so node end sink FS C I am
getting undefined guys, I am getting undefined
but the beauty is now you
can see very clearly and that is key test dot t
one this is a particular file which is
ready here I did not do anything
I did something No, I just made it, I would
like to go here and this is the
initial data, my data is ready, my data is
very easily created here, now you
can do it in another way also. And in a better
way and that's it, I take one more
deliberately cost let's go with path
module, although
I needed it when I was practicing, but now
I released a trick that if you
go to that particular folder. If it was already inside
then there was no need to work so hard
but if there is no one then I will
go here, the path is fine so const file path will be
equal to we have a path dot join we
had studied yesterday itself so I want to make sure
that I basically whatever
this folder of mine is okay we have directory
name comma and this file name in it I
want to make sure that if I ever want If
I go to any file and run this code,
then basically
I go inside that particular folder and
create it. Okay, now the file name, now sorry,
now let me put it here in the file path. So, if I give you
this path module,
we had seen it very well in that video, it is
basically joining and nothing else,
even if it seems difficult, I will add this for you all for a moment,
okay. So now look carefully at the
data that is there at the moment, this data is given back to me,
I do one thing, I write it here, it is
updated, it is ok because at the moment there is
something updated here, otherwise what happens,
now let us see that. Look carefully at one thing
that I Han Han doctor name
was written, right, I got this from FS, OK
right, I got this from FS, now
I will go to CV have this updated, so in
this way, guys, this is your right which does
this work of file sync, if the file does not
exist, If it does, it creates it and
we can add data there. Alright,
perfect, it is done that if the file is
not there then how to create it,
how to add the data in it, it does one thing if it is 8 does
n't happen, okay and now I am doing it,
I go here, I clear it, I
hit enter, then see what
happens now, let me see, look at this,
it still got added, let's go right, whenever you are doing it. Yes, at
that time it is not that much of a problem, but it would
be better if you write 8, okay, this is
my work, but I would
like to keep it as it is and I would like to remove the updated one,
okay,
run it once. If I give it, it is fine,
perfect, there is no problem, okay,
so we are done with our write file, so my one
work is done, let me comment,
I will come here,
pay now, now we have created the file, we have written it
in the file. Whatever data we wanted, we also
wrote it. Now we want to read
what is the data inside that file, what is the content inside that file,
okay, okay, I
will go here, so again the same. Thing is
like cost let's go with a read file you know
and I write here fs3 fi
sync
ok here if you see then only now
data son do not have to add anything in it just
you have to give the path of the file which file you want to read. Do you
want to read and then in which format of the options do
you want to read?
These are the two lines, now
I am cutting them from here and
I am placing them below this at the very top because I like
these. Now both of them will be needed everywhere because
I do not need this particular file but the path of the file,
I have to read the data of this file,
that is why I said, look how
useful it has become now, we have done FS module, today we are
doing but path module, we have done it for a few days.
Earlier I had seen but now we have found it so
easy, now so I will go here,
carefully look at the file path, it is okay, for
now, I will write this much and leave it, tomorrow we will
log in and then we will have this read the file, okay
now I Here I go clear, I
clear it and if it starts then I
take it to the top and we have a node sync FS
and boom guy C. You can see what is
happening here. What is happening here is that the
data you see. It is giving that bup, basically it is
getting in binary format, whatever is the buffer,
it is fine, now there are two ways to fix it,
first, in which format are you
expecting the data, brother, if I
want it in string then do two strings. Two,
one way is this, okay if you do this then
this is the initial data, look at the data,
now I have got another way,
another way was that you cannot write this to string every time,
neither. I
copy this, paste it and comment it
for you guys and there is another
option here, look, they have
given the option here and that is encoded, you
can use it, so I go here and
that is UTF, look at this, there are multiple options,
UTF 8 is fine, if you just
write this, now you do
not need to write all these two strings, you can normally let it
be any data, let it be any kind of data, if you
do this and boom. C Here you
can see that your data is this is the
initial data, I have got it ok,
so in this way you can write the data
and then you can read the data,
let's check it. Out further and a second, there will be
some extra information here
regarding why we are using this,
why we are not using this, so
guys, you
can watch it comfortably by pausing the video, although I will share all this. I will
do it now let's check it out that now
I do not have to append some data,
I have to update the data, okay what to
do, I have to update the data, see,
what do we use for that, append is
a keyword. Which I guess many of you used
when you were working in the array of JavaScript
and at the end if you had to add something then
we used to use append wherever so
here we also have this fs dot fs file and then
sync sorry so it appends data
to a file if the file does not exist it
creates the same file if it does
not exist it will create the file then
here if you syntax If you see, we have the
path of a file, data and then options, then the
path of the file, then again the content of which file
you want to update, the
data with which data you want to update the content of that file
and then again. We
have these options, so I will do one thing, we have
already worked so hard, right, we have this
particular right code, I would like to come here,
okay, now I have already written
two lines at the top. So I have written it for you guys,
so now instead of write file,
append file is ok,
your file is ok, now here also we also have to
go with the append file and then
sync, you are able to see this is the file.
Path of I here pay back slush and then end
because I told you on the same day whenever
you even yes remember if you remember
when we were looking at resolves or when we join
why I have written join The
best part of this join keyword here is that whatever is your
machine, depending on the operating system, it
uses back slash here like back slash
or forward slash because in Windows the
path of the file is shown in your back slash. Let's give it
a second, I would like to show you again,
I am commenting and uncommenting it,
okay and for now I will comment it,
I just
want to show you quickly here, what do you want to say. I am
this is it, so see here, whatever this
particular slash is visible here,
here, here, or here, so how is
this, this is a back slash, this is like something like this,
this is right but you if you Be it a Mac user or a
space character where I
want data on a new line, if I don't write this then
that guy gets added there, so instead
of initial I will go with the updated data.
Okay, rest. There is no problem, I
write here, append file is ok,
perfect so clear and boom guys see this is
the initial data coming ok why is
it coming I guess read yes ok it is coming from here
but well no one this is what we have done It has been appended,
let's check it out, I walk here
and the exam clock and boom guys
see this is the updated data, so we have
seen how you can write, how can we read.
And then how can we update it?
This is very important. You
understand that now
you are playing with the files which are on the server. You understand
that you have to update all the files.
Delete is very useful. Friend, this is right so or I will
do this, I
will comment now at the end and what we need to do is
that now we have to delete the file, now we have to
delete the file, so after deleting it here,
someone Word they did not use which
is fine but instead they used unlink what did
they use unlink basically separate
right so deletes a file by its path
and syntax is very easy like
you just have to give the path of the file you noticed one thing
like Just because we
wrote this particular code, now our life has become so
easy, you understand the full meaning
otherwise you have to work hard, this name is that name, this is this name, that
name is like this. That's it but now it's done there is
no problem here so this is the path of the file I
will come at the bottom so it's like cost
file a delete is ok so we have fs dot
unlink and then sync is ok and here
just find the name of the file friend This is it guys
it's not a big deal just so it was easy
delete the file okay so I go here
and boom guys see now if I
go here see this Poor guy, our test is
gone, what can we do,
right, but there is no one, and there is one more thing that
I wanted to show you, and that is, this
also comes in handy many times, and that is, let's say
that. You have created a file,
now we have seen how you
can update its data, how you can read it,
how you can delete it, but
what if you want to change the name of the file
or if you want to change the folder.
If you want to create They are full of many methods but
I told them in such a way that you can know
how it is working, what is the path,
what is the data, what are the end
options, right,
you will do the rest, see now. Here we need to
rename a file. Now the thing is that the file has
now disappeared. From here, no problem,
I go to our code which is on the right,
I go back to it, control a save,
I hit back. If I give a run, it must have come back.
Look again, the test has come, it's
good. Now, whatever test
data is written here, I will
update it. This means that it will be
known that I have renamed it. Okay
all right and by the way it does
n't interest me
so we go to the bottom okay look so
it's like a
const rename file and fs dot we have a
simple rename sync yes here the rename file is
like this No, it is just rename and then sync, it is
ok
and give the path of the file, basically
see here what is the old path, first of all you have to
give the old path and then now you have to give the new path,
then you can do one thing here
that you can do one thing. New updated a file name will be
equal to like updated test dot
t ext ok and again let's
make it lead for a while because
we can't update the cost so New
update ok, it's fine, do const r do, there is
no problem, so I
create a const new file and then path, f is
like path dot join under underscore,
I will add it to whatever directory we have,
okay there. If we add this then
my work is done, so now my new path name will be
this one. Do you understand what I
am doing, no big deal, so
it's a rename and then file. This is it so
much I have
done so let's make it clear let's
check it out whether the update is done or not now
according to me it should be done and boom guys
see this you can see very clearly that the
updated text I mean sorry or Test d
txt.gz got the job done, I hope its
actually the
same, I will
share its link with you in the description below,
but in tomorrow's
video, now I will tell you this. So
what we did today was your synchronous way and
tomorrow we will see
how you can perform the same piece of code in an asynchronous way with the help of call back functions
but that's all for this video today and I
really hope you You must have liked the video very much
so guys let me know in the
comment section how did you
like the video then please like the video and
share it with your friends and our
next. Target is or 8 lakh subscribers
and present sir keep the comment coming
I like it very much ok so
guys I will meet you now till tomorrow's video guys
thank you so much take care
