Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find only file name from full path of the file in vc++

Suppose there is a CString variable which store the full path of the file.Now I hava to find only file name from if.How to do it in vc++.

CString FileName = "c:\Users\Acer\Desktop\FolderName\abc.dll";

Now I want only abc.dll.

like image 682
Jitendra Avatar asked Jul 17 '12 11:07

Jitendra


People also ask

How do I get the filename from the file path?

To extract filename from the file, we use “GetFileName()” method of “Path” class. This method is used to get the file name and extension of the specified path string. The returned value is null if the file path is null. Syntax: public static string GetFileName (string path);

How to get file name from the full path in VB?

Get the file name from the full path in VB.Net Author Hirendra SisodiyaFormat Aside Posted on March 26, 2014| Categories Visual Basic .Net|Tags System.IO, vb.net In the below there are two methods that can be use to extract the file name from the full file path. Using System.IO.Path.GetFileName

How to get the file name of the specified path string?

The following method is using the System.IO.Path.GetFileName that returns the file name and extension of the specified path string. Private Function GetFileName(ByVal path As String) As String Dim _filename As String = System.IO.Path.GetFileName(path) Return _filename End Function

How to extract the file name from the full file path?

In the below there are two methods that can be use to extract the file name from the full file path. Using System.IO.Path.GetFileName The following method is using the System.IO.Path.GetFileNamethat returns the file name and extension of the specified path string. PrivateFunctionGetFileName(ByValpath AsString)AsStringDim_filename AsString=System.

How do I get the filename of a file in Java?

To extract filename from the file, we use “ GetFileName () ” method of “ Path ” class. This method is used to get the file name and extension of the specified path string. The returned value is null if the file path is null.


4 Answers

You can use PathFindFileName.

Remember that you have to escape the \ character in your path string!

like image 127
Some programmer dude Avatar answered Oct 20 '22 00:10

Some programmer dude


Same as already stated above, but as u are using MFC framework, this would be the way to do it. Though this does not check files existence.

CString path= "c:\\Users\\Acer\\Desktop\\FolderName\\abc.dll";
CString fileName= path.Mid(path.ReverseFind('\\')+1);
like image 36
Aleksandar Avatar answered Oct 20 '22 00:10

Aleksandar


std::string str = "c:\\Users\\Acer\\Desktop\\FolderName\\abc.dll";
std::string res = str.substr( str.find_last_of("\\") + 1 );

Will get you "abs.dll".

like image 25
SingerOfTheFall Avatar answered Oct 19 '22 22:10

SingerOfTheFall


I would use Boost::FileSystem for filename manipulation as it understands what the parts of a name would be. The function you want here would be filename()

If you are just getting the filename you can do this using CString functions. First find the ast backslash using ReverseFind and then Right to get the string wanted.

like image 36
mmmmmm Avatar answered Oct 20 '22 00:10

mmmmmm