WEBVTT 0 00:00.730 --> 00:07.320 Welcome! In this lecture, I'm going to create a project that finds empty files in a given directory. 1 00:07.420 --> 00:10.990 Then it will save the name of those files to a file. 2 00:10.990 --> 00:13.380 Let me show you what we need to do. 3 00:13.420 --> 00:17.340 The first thing I need to do is getting the directory name as an argument. 4 00:17.500 --> 00:22.630 So I'm going to get the command line arguments by skipping the first argument like we usually do so. 5 00:23.850 --> 00:27.030 Let me check the length of the arguments like this. 6 00:27.030 --> 00:34.590 So if there are no arguments I'm going to print a message like this and I'm going to terminate the program. 7 00:34.630 --> 00:39.260 Now I need to find out the files in the given directory. To do that, 8 00:39.340 --> 00:44.700 I need to use a function called the ReadDir from the ioutil package. 9 00:44.720 --> 00:51.040 ioutil package provides practical functions for working with files. First of all, let me show you the 10 00:51.040 --> 00:54.100 documentation of the ReadDir function. 11 00:54.100 --> 00:58.970 So it expects a directory path and then it returns the files and an error value. 12 00:59.020 --> 00:59.940 Right. 13 01:00.070 --> 01:02.260 I'll explain the FileInfo type soon. 14 01:02.260 --> 01:04.300 No worries. 15 01:04.370 --> 01:10.520 OK, let me pass the directory path using the first command line argument. As I said, ReadDir function 16 01:10.760 --> 01:15.030 returns two values. The first value is the files information. 17 01:15.170 --> 01:17.780 So I'm going to store it in the files variable like this. 18 01:18.620 --> 01:20.590 And it also returns an error value. 19 01:20.600 --> 01:23.150 Let me also store it in this error variable. 20 01:23.150 --> 01:25.480 Let me check for the error. 21 01:25.490 --> 01:30.940 Here, if an error occurs it brings the returned error message and terminates the program. 22 01:31.020 --> 01:34.890 Okay let's print the files using a range loop like this. 23 01:36.540 --> 01:39.230 Remember, the files variable is a slice. 24 01:39.240 --> 01:44.300 That is why I can range over it. Now I'm going to print the name of the file 25 01:47.680 --> 01:55.060 But, when I run it, it prints a lot of unnecessary data. It's because, the file variable, here, is not a string. 26 01:55.150 --> 02:01.810 Its type is FileInfo. Its type is FileInfo because the element type of the file slice is a 27 02:01.810 --> 02:03.310 FileInfo slice. 28 02:03.310 --> 02:07.250 So here, it prints the contents of the FileInfo value. 29 02:07.570 --> 02:10.260 But I need the name of the file, right? 30 02:10.300 --> 02:13.060 It is buried inside the FileInfo value. 31 02:13.060 --> 02:18.170 So let's first see what the FileInfo type looks like here. 32 02:18.190 --> 02:18.560 os 33 02:18.570 --> 02:23.160 os is the package name and FileInfo for is the name of the type. 34 02:23.170 --> 02:25.510 So, this is an interface type. 35 02:25.510 --> 02:31.660 I'll talk about the interface types later but very simply put an interface type only describes the methods 36 02:31.840 --> 02:36.490 that you can call on a value that satisfies this interface type. 37 02:36.490 --> 02:42.040 Remember, a method is like a function so you can call it like a function. Here, 38 02:42.080 --> 02:46.150 The documentation is obvious. To find out the name of the file, 39 02:46.190 --> 02:51.510 I'm going to use the Name method on one of the file values that has returned from the ReadDir function. 40 02:52.580 --> 02:57.080 OK let's print the name of the file by using the Name method like so. 41 02:57.450 --> 03:04.800 Remember, I can call the Name method because the file value has this method through the FileInfo interface. 42 03:05.020 --> 03:06.810 OK? All right. 43 03:06.850 --> 03:11.720 Let me run it. As you can see, it prints the files in my current directory. 44 03:11.910 --> 03:15.730 Cool! But I want to print only the empty file, right? 45 03:15.800 --> 03:22.270 So please create a folder and name it as "files" and put some empty and non-empty files in it. 46 03:22.300 --> 03:23.010 OK? 47 03:23.060 --> 03:28.070 By the way, if you don't know how to create empty files, please download the zip file that I've prepared 48 03:28.070 --> 03:32.610 for you from the resources of this lecture and then extract it under the "files" directory. 49 03:32.780 --> 03:33.450 Just that. 50 03:33.560 --> 03:34.800 Here are my files. 51 03:35.000 --> 03:39.500 As you can see, empty files have zero bytes so they are empty. 52 03:39.500 --> 03:41.610 The other files have 23 bytes. 53 03:41.660 --> 03:42.860 So they are not empty. 54 03:43.600 --> 03:44.920 Okay the files are ready. 55 03:44.960 --> 03:46.550 Let's get back to the code now. 56 03:47.080 --> 03:49.670 OK let me delete this line first. 57 03:49.820 --> 03:54.790 Instead, I'm going to save the name of the file in this string variable like so. 58 03:54.870 --> 03:58.470 Now let's check out the methods of the FileInfo again. 59 03:58.470 --> 04:04.500 There is a Size method and according to the documentation the Size method returns the length of a file 60 04:04.500 --> 04:07.490 in bytes as an int64 value. 61 04:07.680 --> 04:13.840 So I can call it on the file value because the file value's type is FileInfo which is an interface type. 62 04:14.040 --> 04:14.600 Okay. 63 04:14.700 --> 04:17.730 So now I'm gonna check whether the file is empty or not 64 04:17.730 --> 04:24.660 by comparing its size to zero within this if statement. If so I'm going to print the name of the file 65 04:24.660 --> 04:27.840 to see whether it really finds the empty files or not. 66 04:27.840 --> 04:28.170 Okay. 67 04:28.180 --> 04:35.690 Let me run it. Now I'm going to pass the files directory to my program like so. VoilĂ ! It finds only the empty 68 04:35.690 --> 04:36.720 files. 69 04:36.910 --> 04:37.420 Cool. 70 04:37.580 --> 04:41.060 Let me also run it with a directory that doesn't exist. 71 04:41.060 --> 04:47.180 Cool! It also prints the error message correctly. So, as a summary, let me show you what we have done so 72 04:47.180 --> 04:47.520 far once more. 73 04:47.510 --> 04:54.390 Let's say I have a directory that contains non-empty and empty files such as these ones. 74 04:54.680 --> 04:57.500 First I call the ReadDir function. 75 04:57.500 --> 05:04.490 Then it returns me a []FileInfo. Each FileInfo value contains methods like the Name and the Size. 76 05:05.120 --> 05:09.920 Using one of the FileInfo values, I can access the Name and the Size of a file. 77 05:10.270 --> 05:16.490 The Name method gives me the name of the file, and the Size method gives me the total size of that file 78 05:16.490 --> 05:18.010 in bytes. 79 05:18.080 --> 05:19.790 All right! That's all for now. 80 05:20.000 --> 05:25.310 In the next lecture, I'm going to show you how to write the name of the empty files to a file. 81 05:25.370 --> 05:25.990 See you there! 82 05:26.030 --> 05:26.240 Bye!