Get Filename from Path in C++

This article explains the various ways to get filename from path using C++ programs. The examples will also describe ways to remove extensions as well if such needs arise.

Get Filename From Path in C++

Finding a file name from its file path is a simple task. The program needs to find the last delimiter, and delete everything that occurs before it.

If extensions are needed to be removed, the program searches for a period(.) and deletes everything that occurs after that. Let’s look at some examples to understand how to get filename from path.

Using find_last_of and substr methods

To get filename from path, the program uses two methods – clear_slash() to find delimiters and extension_removal() to remove the extensions.

Output:

Filename With extension = get_filename.md
Just the Filename = get_filename

Let’s break down the code

The first two lines of code import the dependencies of the program – library string and iostream. In the third line, the scope of std is included, which means datatypes like string can be used without using any prefix like std::string.

Method clear_slash():

A string method clear_slash is created with two string parameters – path_of_file which stores the file path and d_slash. The variable d_slash is initialized with the value '/\\' which means double slash.

The method separates the file name from the file path. It does it by finding the last delimiter – \\ from the file path and removing the string before it.

To find the index of the last delimiter, the function find_last_of() is used. The syntax path_of_file.find_last_of(d_slash) takes the value from d_slash and searches the last occurrence of it in variable path_of_file. It returns the index of the last double slashes, and it is stored inside the variable index_of_slash of datatype – size_t.

The substr() function returns substring from a given string. The syntax path_of_file.substr(index_of_slash + 1) returns the string that are after the index of \\ from the variable path_of_file and stores it inside a new string variable file_name.

Lastly, the variable file_name is returned.

Method extension_removal():

The second method is used to remove the extension from the file path. A string method extension_removal having a string variable name_of_file as the parameter will get the file name passed to this method and it will return the filename without extension.

Removing extensions is similar to removing delimiters. First, a variable index_of_period of datatype size_t is created which stores the index of period(.) searched from the name_of_file using the function find_last_of().

The string available before the period(.) is saved inside a new string variable file_without_extensionusing the substr function, starting from the 0th index to index_of_period.

Finally, the variable file_without_extension is returned. Now, we will look at how to use these two methods to get filename from path.

main_function:

The path of the file is stored inside the variable file_address. In the second line, the method clear_slash() uses the file_address to pass the file path, separate the path, and then print the returned value.

To remove extension as well from the file path, both the methods are nested and called with file_address passed as an argument. It returns just the filename from the file path and it is printed.

Using Templates

A template is used to get filename from path when different instances of string are used, like – string or wstring(strings with a wider sequence than basic strings).

Output:

get_filename.md
C:\Users\Win 10\Documents\get_filename
get_filename

Import packages are the same as the last program.

A template class T is defined. The first method remove_slash is created with T as a datatype. It has parameters – path_of_file and d_slash with datatype T.

The second method extension_removal created with datatype T has parameter name_of_file.

Inside the main function, the string variable filepath1 stores the address of the file with remove_slash as its identifier. Similarly, filepath2 stores address with extension_removal as its identifier.

The first method remove_slash is called with filepath1 passed as its argument and the returned value is printed. Then extension_removal is called with filepath2 sent as its argument. In nested methods, both file path variables can be used.

This example can be used with other specialized string types as well like wstring, etc to get filename from path.

Using filesysystem library [ C++ 17 ]

If the system has C++ 17 or later, the easiest way is to use the filesystem library to get filename from path.

The filesystem library is assigned to a keyword obj using the namespace.

To get the file name, two functions are used:

  • filename() – Returns the filename with extension.
  • stem() – Returns just the filename.

Let’s assume a variable filename has a file path like – "C:\\MyDirectory\\MyFile.bat". To get the filename from path, the following syntax is used:

This will return – "MyFile.bat". To remove extensions as well, use the command:

This will return – "MyFile"

The below program has three imports – iostream, string, and the filesystem library and it uses a list of inputs to find out what the filesystem functions return for specific inputs:

Output:

“pic1.jpg”

“pic1”

“app.exe”

“app”

Conclusion

This program provides a brief description of the various ways to get filename from path using C++. The reader will be able to use the concepts easily after reading the article.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *