[No Audio]
This tutorial is about some basic functions
which are provided in the standard library
for manipulating directory and path related
functions, so let's start learning them.
First I will bring the relevant modules into scope.
[No Audio]
First, we will cover some path related
functions, I will create a path using the new
function in the path module.
[No Audio]
The r before the text makes sure that the text is being
treated as raw string, thereby escaping the
effects of escape sequences. The parent
function can be used to access the directory
information in which a specific file is
residing based on the path information of the
file. Let us use this to display the directory
in which the my_txt.txt file is residing.
[No Audio]
Please note that, it will return an option, so
we have to unwrap it. Let us cargo run this now.
[No Audio]
Okay, great. To extract the file name, the
module provides the file_stem
function, and to access the extension of the
file the module provides the extension
function. Let us use them inside the print statement.
[No Audio]
Sometimes we may need to create our own path
for arranging items in our own defined
directory structure. We will create a PathBuf
variable for this purpose. For instance,
I want to create a new path for some files in the D drive.
[No Audio]
Next, I can add folders or sub directories to
this path by simplifying using the push function.
[No Audio]
The final item will be the name of the file.
[No Audio]
Lastly, I will add the extension
to path also by using the extension function.
[No Audio]
Let us print the full path now.
[No Audio]
Please note that although there are a couple of
backslashes when we print the path, but this
path is readable in Windows, and you can use it
to create a file. The same can be done using
iterators and vectors. First I will create a
vector containing raw strings in sequential order.
[No Audio]
Next, I will iterate through all the elements
using the iter function and then collect it
in a PathBuf types.
[No Audio]
Finally, let us print the path.
Let us cargo run this now.
[No Audio]
Both methods produces the same path.
Sometimes it is useful to check if a
certain directory exists in the file system
or not, the is_directory function
is used for this purpose. For instance, I will
create a path variable first.
[No Audio]
Now to check if this path contains
a valid directory on the system,
I will use the is_dir function.
[No Audio]
Let us cargo run this now.
[No Audio]
Similar to the is directory, there is a function
called is_file for checking if a
certain, if a certain path is pointing to a
valid file or not. Let us use it by first
creating a path, that is pointing to a valid
file on the system.
[No Audio]
Next, I will call the is_file
function inside a print statement.
[No Audio]
Let us cargo run this now.
[No Audio]
The metadata is another useful function which
provides the details of a file that is
pointed to by the path variable, let us use it.
[No Audio]
This function returns a result enum, and
therefore needs to unwrap to access its
fields. The individual information about the
file can be accessed using individual
functions on the data. For instance, to
display the file type, we will use the
file _type function.
[No Audio]
Next we will display the
bytes the file is taking in the memory,
and the permissions and modified and created properties.
[No Audio]
Let us cargo run this now
to see the details of the file.
[No Audio]
That was all for now related to the basic path
related functions. This will cover majority
of the needs that you may encounter during
Rust programming. Let us now cover some of
the directory related functions. First, I will
comment out the code so far.
[No Audio]
The very first function we would like to
learn is, to display all the files in a
certain directory, I will first create a path variable.
[No Audio]
Next, we will use the read_dir
function which enables us to loop through all
the files. The read_dir directory
returns a result with read directory
structure, which contains the individual
entries. When we iterate through this
structure, it provides the individual
directory entries. Finally, we use the expect
function which will be called in case the
read_dir call fails, which may
happen when the directory does not exist in
the memory. Inside the loop, I will display the files.
[No Audio]
We unwrapped the files as it is a result, and
then use the path function to display the path
of the file. Let us cargo run this now,
to display all the paths.
[No Audio]
To display the current directory information,
the environment module provides a function
called current_directory. Let us
use this to obtain the path of the current_dir.
[No Audio]
Let us also display the current
path in a print statement.
[No Audio]
I will comment out the previous code before executing.
[No Audio]
Let us go go run this now.
[No Audio]
Next, we will learn how to create a directory. The
create_dir function in the fs
module will be used in this regards. I will
create a new directory by calling the
create_dir function inside the print statement.
[No Audio]
This will create a new empty directory at
the provided path. To create a directory and
subdirectories inside them at the same time,
we will use the create_dir_all
function. Let us use it inside a print statement.
[No Audio]
Please note that, this will create a directory,
and all its missing parents at the same time.
This means that, since the level one was not
created already, and which is a parent
directory of the level two, so this function
will create this also. However, since Rust
one was already created, and it is also one of
the parent directory, but since it already
exists, so therefore it will not be created,
let us cargo run this now.
[No Audio]
Both these function returns an Ok variant
when the operation is successful. Let us
inspect the D drive to see if the files have been created.
[No Audio]
You may note that we have rust1,
what if we open it it contains the level1
and then level2. Let us now move to the
next topic, which is removing files and
directories. First I will comment out the code so far.
[No Audio]
To remove a directory, there is a
remove_dir function, this function
can be used to remove a specific directory
Let us use it inside the print statement.
[No Audio]
This function will not work, if the target
directory contains some files or
subdirectories. This means that it will only
work if the target directory is empty and
does not contain anything.
[No Audio]
For instance, if I call this function
by providing the rust1
directory, then it will not work.
[No Audio]
You may note that, the first call was
successful, however the second call returned
an error, because the directory was not empty.
To forcefully delete everything from the
directory, even if it contains some files or
subdirectories, we will use the remove
directory all function.
[No Audio]
This will erase all the directories
even if it contains some
items. Let us cargo run this.
[No Audio]
The last function in this regards is, to
remove a specific file, the remove_file
function will be used for this purpose.
Let us use it inside a print statement to
delete a file that we created earlier.
[No Audio]
The function rename can be used to rename a
file. Let us explain its syntax in a print statement.
[No Audio]
Previous is the file for whom you
want to change the name, and the new is the
new name for the file. Please do not forget
to provide the full name, along with the
extension while renaming a file. The last
operation in this tutorial is about copying
the contents of one file to another. The copy
function in the fs module is used for this
purpose. Let us use it inside the print
statement to elaborate its syntax.
[No Audio]
This will essentially copy the contents of the file
new1.txt to new2.txt, deleting
everything from new2 and , ill only contains
the contents of the new1. That is it for
this particular tutorial. See you again, and
until then happy Rust programming.
[No Audio]