1 00:00:00,000 --> 00:00:06,866 [No Audio] 2 00:00:06,867 --> 00:00:09,666 This tutorial is about some basic functions 3 00:00:09,667 --> 00:00:11,500 which are provided in the standard library 4 00:00:11,501 --> 00:00:13,933 for manipulating directory and path related 5 00:00:13,934 --> 00:00:15,866 functions, so let's start learning them. 6 00:00:16,100 --> 00:00:18,766 First I will bring the relevant modules into scope. 7 00:00:18,767 --> 00:00:25,133 [No Audio] 8 00:00:25,134 --> 00:00:27,900 First, we will cover some path related 9 00:00:27,901 --> 00:00:31,166 functions, I will create a path using the new 10 00:00:31,167 --> 00:00:33,000 function in the path module. 11 00:00:33,001 --> 00:00:36,433 [No Audio] 12 00:00:36,434 --> 00:00:39,866 The r before the text makes sure that the text is being 13 00:00:39,867 --> 00:00:42,133 treated as raw string, thereby escaping the 14 00:00:42,134 --> 00:00:45,100 effects of escape sequences. The parent 15 00:00:45,101 --> 00:00:47,200 function can be used to access the directory 16 00:00:47,201 --> 00:00:49,366 information in which a specific file is 17 00:00:49,367 --> 00:00:51,533 residing based on the path information of the 18 00:00:51,534 --> 00:00:53,966 file. Let us use this to display the directory 19 00:00:53,967 --> 00:00:58,033 in which the my_txt.txt file is residing. 20 00:00:58,034 --> 00:01:03,633 [No Audio] 21 00:01:03,634 --> 00:01:05,933 Please note that, it will return an option, so 22 00:01:05,934 --> 00:01:08,633 we have to unwrap it. Let us cargo run this now. 23 00:01:08,634 --> 00:01:13,700 [No Audio] 24 00:01:13,701 --> 00:01:16,800 Okay, great. To extract the file name, the 25 00:01:16,801 --> 00:01:18,866 module provides the file_stem 26 00:01:18,867 --> 00:01:20,966 function, and to access the extension of the 27 00:01:20,967 --> 00:01:23,000 file the module provides the extension 28 00:01:23,001 --> 00:01:26,100 function. Let us use them inside the print statement. 29 00:01:26,101 --> 00:01:33,833 [No Audio] 30 00:01:33,834 --> 00:01:36,766 Sometimes we may need to create our own path 31 00:01:36,767 --> 00:01:39,300 for arranging items in our own defined 32 00:01:39,301 --> 00:01:42,266 directory structure. We will create a PathBuf 33 00:01:42,267 --> 00:01:44,300 variable for this purpose. For instance, 34 00:01:44,301 --> 00:01:47,433 I want to create a new path for some files in the D drive. 35 00:01:47,434 --> 00:01:54,333 [No Audio] 36 00:01:54,334 --> 00:01:57,100 Next, I can add folders or sub directories to 37 00:01:57,101 --> 00:01:59,766 this path by simplifying using the push function. 38 00:01:59,767 --> 00:02:04,133 [No Audio] 39 00:02:04,134 --> 00:02:07,500 The final item will be the name of the file. 40 00:02:07,501 --> 00:02:11,233 [No Audio] 41 00:02:11,234 --> 00:02:13,666 Lastly, I will add the extension 42 00:02:13,667 --> 00:02:16,333 to path also by using the extension function. 43 00:02:16,334 --> 00:02:20,333 [No Audio] 44 00:02:20,334 --> 00:02:22,566 Let us print the full path now. 45 00:02:22,567 --> 00:02:25,733 [No Audio] 46 00:02:25,734 --> 00:02:27,900 Please note that although there are a couple of 47 00:02:27,901 --> 00:02:30,733 backslashes when we print the path, but this 48 00:02:30,734 --> 00:02:33,300 path is readable in Windows, and you can use it 49 00:02:33,301 --> 00:02:35,866 to create a file. The same can be done using 50 00:02:35,867 --> 00:02:38,300 iterators and vectors. First I will create a 51 00:02:38,301 --> 00:02:41,000 vector containing raw strings in sequential order. 52 00:02:41,001 --> 00:02:46,366 [No Audio] 53 00:02:46,367 --> 00:02:48,500 Next, I will iterate through all the elements 54 00:02:48,501 --> 00:02:50,666 using the iter function and then collect it 55 00:02:50,667 --> 00:02:52,466 in a PathBuf types. 56 00:02:52,467 --> 00:02:57,566 [No Audio] 57 00:02:57,567 --> 00:02:59,266 Finally, let us print the path. 58 00:02:59,267 --> 00:03:00,966 Let us cargo run this now. 59 00:03:00,967 --> 00:03:03,233 [No Audio] 60 00:03:03,234 --> 00:03:05,533 Both methods produces the same path. 61 00:03:05,666 --> 00:03:07,866 Sometimes it is useful to check if a 62 00:03:07,867 --> 00:03:10,066 certain directory exists in the file system 63 00:03:10,067 --> 00:03:12,733 or not, the is_directory function 64 00:03:12,734 --> 00:03:15,166 is used for this purpose. For instance, I will 65 00:03:15,167 --> 00:03:17,133 create a path variable first. 66 00:03:17,134 --> 00:03:21,300 [No Audio] 67 00:03:21,301 --> 00:03:23,100 Now to check if this path contains 68 00:03:23,101 --> 00:03:24,633 a valid directory on the system, 69 00:03:24,634 --> 00:03:27,566 I will use the is_dir function. 70 00:03:27,567 --> 00:03:30,600 [No Audio] 71 00:03:30,601 --> 00:03:32,666 Let us cargo run this now. 72 00:03:32,667 --> 00:03:36,566 [No Audio] 73 00:03:36,567 --> 00:03:39,566 Similar to the is directory, there is a function 74 00:03:39,567 --> 00:03:41,866 called is_file for checking if a 75 00:03:41,867 --> 00:03:44,666 certain, if a certain path is pointing to a 76 00:03:44,667 --> 00:03:47,433 valid file or not. Let us use it by first 77 00:03:47,434 --> 00:03:49,533 creating a path, that is pointing to a valid 78 00:03:49,534 --> 00:03:50,866 file on the system. 79 00:03:50,867 --> 00:03:56,533 [No Audio] 80 00:03:56,534 --> 00:03:58,700 Next, I will call the is_file 81 00:03:58,701 --> 00:04:00,533 function inside a print statement. 82 00:04:00,534 --> 00:04:05,666 [No Audio] 83 00:04:05,667 --> 00:04:07,400 Let us cargo run this now. 84 00:04:07,401 --> 00:04:13,566 [No Audio] 85 00:04:13,567 --> 00:04:15,933 The metadata is another useful function which 86 00:04:15,934 --> 00:04:17,700 provides the details of a file that is 87 00:04:17,701 --> 00:04:20,666 pointed to by the path variable, let us use it. 88 00:04:20,667 --> 00:04:27,866 [No Audio] 89 00:04:27,867 --> 00:04:30,266 This function returns a result enum, and 90 00:04:30,267 --> 00:04:33,033 therefore needs to unwrap to access its 91 00:04:33,034 --> 00:04:35,466 fields. The individual information about the 92 00:04:35,467 --> 00:04:37,200 file can be accessed using individual 93 00:04:37,201 --> 00:04:39,233 functions on the data. For instance, to 94 00:04:39,234 --> 00:04:41,400 display the file type, we will use the 95 00:04:41,401 --> 00:04:43,433 file _type function. 96 00:04:43,434 --> 00:04:46,766 [No Audio] 97 00:04:46,767 --> 00:04:48,333 Next we will display the 98 00:04:48,334 --> 00:04:50,233 bytes the file is taking in the memory, 99 00:04:50,234 --> 00:04:53,733 and the permissions and modified and created properties. 100 00:04:53,734 --> 00:05:01,100 [No Audio] 101 00:05:01,101 --> 00:05:02,400 Let us cargo run this now 102 00:05:02,401 --> 00:05:03,800 to see the details of the file. 103 00:05:03,801 --> 00:05:08,666 [No Audio] 104 00:05:08,667 --> 00:05:11,400 That was all for now related to the basic path 105 00:05:11,401 --> 00:05:14,233 related functions. This will cover majority 106 00:05:14,234 --> 00:05:16,233 of the needs that you may encounter during 107 00:05:16,234 --> 00:05:18,300 Rust programming. Let us now cover some of 108 00:05:18,301 --> 00:05:20,700 the directory related functions. First, I will 109 00:05:20,701 --> 00:05:22,266 comment out the code so far. 110 00:05:22,267 --> 00:05:28,300 [No Audio] 111 00:05:28,301 --> 00:05:29,966 The very first function we would like to 112 00:05:29,967 --> 00:05:31,700 learn is, to display all the files in a 113 00:05:31,701 --> 00:05:35,566 certain directory, I will first create a path variable. 114 00:05:35,567 --> 00:05:41,000 [No Audio] 115 00:05:41,001 --> 00:05:43,500 Next, we will use the read_dir 116 00:05:43,501 --> 00:05:45,533 function which enables us to loop through all 117 00:05:45,534 --> 00:05:49,066 the files. The read_dir directory 118 00:05:49,067 --> 00:05:51,500 returns a result with read directory 119 00:05:51,501 --> 00:05:54,233 structure, which contains the individual 120 00:05:54,234 --> 00:05:56,500 entries. When we iterate through this 121 00:05:56,533 --> 00:05:58,466 structure, it provides the individual 122 00:05:58,467 --> 00:06:02,400 directory entries. Finally, we use the expect 123 00:06:02,401 --> 00:06:04,300 function which will be called in case the 124 00:06:04,301 --> 00:06:06,766 read_dir call fails, which may 125 00:06:06,767 --> 00:06:08,766 happen when the directory does not exist in 126 00:06:08,767 --> 00:06:11,966 the memory. Inside the loop, I will display the files. 127 00:06:11,967 --> 00:06:17,233 [No Audio] 128 00:06:17,234 --> 00:06:20,100 We unwrapped the files as it is a result, and 129 00:06:20,101 --> 00:06:22,400 then use the path function to display the path 130 00:06:22,401 --> 00:06:24,566 of the file. Let us cargo run this now, 131 00:06:24,567 --> 00:06:25,833 to display all the paths. 132 00:06:25,834 --> 00:06:31,733 [No Audio] 133 00:06:31,734 --> 00:06:33,766 To display the current directory information, 134 00:06:33,767 --> 00:06:36,066 the environment module provides a function 135 00:06:36,067 --> 00:06:38,566 called current_directory. Let us 136 00:06:38,567 --> 00:06:41,066 use this to obtain the path of the current_dir. 137 00:06:41,067 --> 00:06:46,200 [No Audio] 138 00:06:46,201 --> 00:06:47,900 Let us also display the current 139 00:06:47,901 --> 00:06:49,200 path in a print statement. 140 00:06:49,201 --> 00:06:56,033 [No Audio] 141 00:06:56,034 --> 00:06:59,233 I will comment out the previous code before executing. 142 00:06:59,234 --> 00:07:01,600 [No Audio] 143 00:07:01,601 --> 00:07:03,200 Let us go go run this now. 144 00:07:03,201 --> 00:07:05,500 [No Audio] 145 00:07:05,501 --> 00:07:08,233 Next, we will learn how to create a directory. The 146 00:07:08,234 --> 00:07:10,600 create_dir function in the fs 147 00:07:10,601 --> 00:07:13,333 module will be used in this regards. I will 148 00:07:13,334 --> 00:07:14,833 create a new directory by calling the 149 00:07:14,834 --> 00:07:17,666 create_dir function inside the print statement. 150 00:07:17,667 --> 00:07:22,666 [No Audio] 151 00:07:22,667 --> 00:07:25,033 This will create a new empty directory at 152 00:07:25,034 --> 00:07:27,600 the provided path. To create a directory and 153 00:07:27,601 --> 00:07:30,333 subdirectories inside them at the same time, 154 00:07:30,334 --> 00:07:33,233 we will use the create_dir_all 155 00:07:33,234 --> 00:07:36,433 function. Let us use it inside a print statement. 156 00:07:36,434 --> 00:07:43,800 [No Audio] 157 00:07:43,801 --> 00:07:45,866 Please note that, this will create a directory, 158 00:07:45,867 --> 00:07:48,533 and all its missing parents at the same time. 159 00:07:48,833 --> 00:07:51,100 This means that, since the level one was not 160 00:07:51,101 --> 00:07:53,766 created already, and which is a parent 161 00:07:53,767 --> 00:07:56,000 directory of the level two, so this function 162 00:07:56,001 --> 00:07:58,966 will create this also. However, since Rust 163 00:07:58,967 --> 00:08:01,966 one was already created, and it is also one of 164 00:08:01,967 --> 00:08:04,500 the parent directory, but since it already 165 00:08:04,501 --> 00:08:06,933 exists, so therefore it will not be created, 166 00:08:06,934 --> 00:08:08,300 let us cargo run this now. 167 00:08:08,301 --> 00:08:14,300 [No Audio] 168 00:08:14,301 --> 00:08:17,133 Both these function returns an Ok variant 169 00:08:17,266 --> 00:08:19,500 when the operation is successful. Let us 170 00:08:19,501 --> 00:08:22,466 inspect the D drive to see if the files have been created. 171 00:08:22,467 --> 00:08:26,933 [No Audio] 172 00:08:26,934 --> 00:08:28,800 You may note that we have rust1, 173 00:08:28,801 --> 00:08:30,833 what if we open it it contains the level1 174 00:08:30,834 --> 00:08:33,433 and then level2. Let us now move to the 175 00:08:33,434 --> 00:08:35,265 next topic, which is removing files and 176 00:08:35,266 --> 00:08:38,265 directories. First I will comment out the code so far. 177 00:08:38,266 --> 00:08:44,232 [No Audio] 178 00:08:44,233 --> 00:08:45,933 To remove a directory, there is a 179 00:08:45,934 --> 00:08:48,400 remove_dir function, this function 180 00:08:48,401 --> 00:08:50,966 can be used to remove a specific directory 181 00:08:51,500 --> 00:08:53,466 Let us use it inside the print statement. 182 00:08:53,467 --> 00:09:00,200 [No Audio] 183 00:09:00,201 --> 00:09:02,666 This function will not work, if the target 184 00:09:02,667 --> 00:09:04,533 directory contains some files or 185 00:09:04,534 --> 00:09:07,200 subdirectories. This means that it will only 186 00:09:07,201 --> 00:09:09,300 work if the target directory is empty and 187 00:09:09,301 --> 00:09:11,000 does not contain anything. 188 00:09:11,001 --> 00:09:15,100 [No Audio] 189 00:09:15,101 --> 00:09:16,833 For instance, if I call this function 190 00:09:16,834 --> 00:09:18,066 by providing the rust1 191 00:09:18,067 --> 00:09:20,133 directory, then it will not work. 192 00:09:20,134 --> 00:09:26,266 [No Audio] 193 00:09:26,267 --> 00:09:27,900 You may note that, the first call was 194 00:09:27,901 --> 00:09:29,966 successful, however the second call returned 195 00:09:30,000 --> 00:09:32,500 an error, because the directory was not empty. 196 00:09:32,933 --> 00:09:35,133 To forcefully delete everything from the 197 00:09:35,134 --> 00:09:37,666 directory, even if it contains some files or 198 00:09:37,667 --> 00:09:39,900 subdirectories, we will use the remove 199 00:09:39,901 --> 00:09:41,633 directory all function. 200 00:09:41,634 --> 00:09:45,500 [No Audio] 201 00:09:45,501 --> 00:09:47,333 This will erase all the directories 202 00:09:47,334 --> 00:09:48,700 even if it contains some 203 00:09:48,701 --> 00:09:51,133 items. Let us cargo run this. 204 00:09:51,134 --> 00:09:56,866 [No Audio] 205 00:09:56,867 --> 00:09:58,700 The last function in this regards is, to 206 00:09:58,701 --> 00:10:01,766 remove a specific file, the remove_file 207 00:10:01,767 --> 00:10:03,866 function will be used for this purpose. 208 00:10:03,867 --> 00:10:05,700 Let us use it inside a print statement to 209 00:10:05,701 --> 00:10:07,800 delete a file that we created earlier. 210 00:10:07,801 --> 00:10:13,566 [No Audio] 211 00:10:13,567 --> 00:10:15,566 The function rename can be used to rename a 212 00:10:15,567 --> 00:10:18,833 file. Let us explain its syntax in a print statement. 213 00:10:18,834 --> 00:10:23,066 [No Audio] 214 00:10:23,067 --> 00:10:25,133 Previous is the file for whom you 215 00:10:25,134 --> 00:10:27,400 want to change the name, and the new is the 216 00:10:27,401 --> 00:10:29,833 new name for the file. Please do not forget 217 00:10:29,834 --> 00:10:31,966 to provide the full name, along with the 218 00:10:31,967 --> 00:10:35,533 extension while renaming a file. The last 219 00:10:35,534 --> 00:10:37,700 operation in this tutorial is about copying 220 00:10:37,701 --> 00:10:40,266 the contents of one file to another. The copy 221 00:10:40,267 --> 00:10:43,500 function in the fs module is used for this 222 00:10:43,501 --> 00:10:45,500 purpose. Let us use it inside the print 223 00:10:45,501 --> 00:10:47,433 statement to elaborate its syntax. 224 00:10:47,434 --> 00:10:51,166 [No Audio] 225 00:10:51,167 --> 00:10:53,766 This will essentially copy the contents of the file 226 00:10:53,767 --> 00:10:57,566 new1.txt to new2.txt, deleting 227 00:10:57,567 --> 00:11:00,600 everything from new2 and , ill only contains 228 00:11:00,601 --> 00:11:03,800 the contents of the new1. That is it for 229 00:11:03,801 --> 00:11:05,766 this particular tutorial. See you again, and 230 00:11:05,767 --> 00:11:07,966 until then happy Rust programming. 231 00:11:07,967 --> 00:11:14,233 [No Audio]