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.
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);
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
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
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.
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.
You can use PathFindFileName
.
Remember that you have to escape the \
character in your path string!
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);
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".
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With