Welcome! In this lecture, I'm going to create a project that finds empty files in a given directory.
Then it will save the name of those files to a file.
Let me show you what we need to do.
The first thing I need to do is getting the directory name as an argument.
So I'm going to get the command line arguments by skipping the first argument like we usually do so.
Let me check the length of the arguments like this.
So if there are no arguments I'm going to print a message like this and I'm going to terminate the program.
Now I need to find out the files in the given directory. To do that,
I need to use a function called the ReadDir from the ioutil package.
ioutil package provides practical functions for working with files. First of all, let me show you the
documentation of the ReadDir function.
So it expects a directory path and then it returns the files and an error value.
Right.
I'll explain the FileInfo type soon.
No worries.
OK, let me pass the directory path using the first command line argument. As I said, ReadDir function
returns two values. The first value is the files information.
So I'm going to store it in the files variable like this.
And it also returns an error value.
Let me also store it in this error variable.
Let me check for the error.
Here, if an error occurs it brings the returned error message and terminates the program.
Okay let's print the files using a range loop like this.
Remember, the files variable is a slice.
That is why I can range over it. Now I'm going to print the name of the file
But, when I run it, it prints a lot of unnecessary data. It's because, the file variable, here, is not a string.
Its type is FileInfo. Its type is FileInfo because the element type of the file slice is a
FileInfo slice.
So here, it prints the contents of the FileInfo value.
But I need the name of the file, right?
It is buried inside the FileInfo value.
So let's first see what the FileInfo type looks like here.
os
os is the package name and FileInfo for is the name of the type.
So, this is an interface type.
I'll talk about the interface types later but very simply put an interface type only describes the methods
that you can call on a value that satisfies this interface type.
Remember, a method is like a function so you can call it like a function. Here,
The documentation is obvious. To find out the name of the file,
I'm going to use the Name method on one of the file values that has returned from the ReadDir function.
OK let's print the name of the file by using the Name method like so.
Remember, I can call the Name method because the file value has this method through the FileInfo interface.
OK? All right.
Let me run it. As you can see, it prints the files in my current directory.
Cool! But I want to print only the empty file, right?
So please create a folder and name it as "files" and put some empty and non-empty files in it.
OK?
By the way, if you don't know how to create empty files, please download the zip file that I've prepared
for you from the resources of this lecture and then extract it under the "files" directory.
Just that.
Here are my files.
As you can see, empty files have zero bytes so they are empty.
The other files have 23 bytes.
So they are not empty.
Okay the files are ready.
Let's get back to the code now.
OK let me delete this line first.
Instead, I'm going to save the name of the file in this string variable like so.
Now let's check out the methods of the FileInfo again.
There is a Size method and according to the documentation the Size method returns the length of a file
in bytes as an int64 value.
So I can call it on the file value because the file value's type is FileInfo which is an interface type.
Okay.
So now I'm gonna check whether the file is empty or not
by comparing its size to zero within this if statement. If so I'm going to print the name of the file
to see whether it really finds the empty files or not.
Okay.
Let me run it. Now I'm going to pass the files directory to my program like so. Voilà! It finds only the empty
files.
Cool.
Let me also run it with a directory that doesn't exist.
Cool! It also prints the error message correctly. So, as a summary, let me show you what we have done so
far once more.
Let's say I have a directory that contains non-empty and empty files such as these ones.
First I call the ReadDir function.
Then it returns me a []FileInfo. Each FileInfo value contains methods like the Name and the Size.
Using one of the FileInfo values, I can access the Name and the Size of a file.
The Name method gives me the name of the file, and the Size method gives me the total size of that file
in bytes.
All right! That's all for now.
In the next lecture, I'm going to show you how to write the name of the empty files to a file.
See you there!
Bye!