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.
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.
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.
The code below demonstrate extracting a file name from a full path
#include <iostream>
#include <cstdlib>
#include <string>
#include <algorithm>
std::string get_file_name_from_full_path(const std::string& file_path)
{
std::string file_name;
std::string::const_reverse_iterator it = std::find(file_path.rbegin(), file_path.rend(), '\\');
if (it != file_path.rend())
{
file_name.assign(file_path.rbegin(), it);
std::reverse(file_name.begin(), file_name.end());
return file_name;
}
else
return file_name;
}
int main()
{
std::string file_path = "c:\\Users\\Acer\\Desktop\\FolderName\\abc.dll";
std::cout << get_file_name_from_full_path(file_path) << std::endl;
return EXIT_SUCCESS;
}