根据文件完整路径名,解析文件路径、文件名、后缀名
std::string ExtractFilePath(const char* lpszFileName)
{
char _Drive[_MAX_DRIVE] = { 0 };
char _Dir[_MAX_DIR] = { 0 };
char _Filename[_MAX_FNAME] = { 0 };
char _Ext[_MAX_EXT] = { 0 };
char _Path[_MAX_PATH] = { 0 };
_tsplitpath_s(lpszFileName, _Drive, _MAX_DRIVE, _Dir, _MAX_DIR, _Filename, _MAX_FNAME, _Ext, _MAX_EXT);
sprintf_s(_Path, _MAX_PATH, "%s%s", _Drive, _Dir);
std::string sRet = _Path;
return sRet;
}
std::string ExtractFileName(const char* lpszFileName)
{
char _Drive[_MAX_DRIVE] = { 0 };
char _Dir[_MAX_DIR] = { 0 };
char _Filename[_MAX_FNAME] = { 0 };
char _Ext[_MAX_EXT] = { 0 };
_tsplitpath_s(lpszFileName, _Drive, _MAX_DRIVE, _Dir, _MAX_DIR, _Filename, _MAX_FNAME, _Ext, _MAX_EXT);
strcat_s(_Filename, _Ext);
std::string sRet = _Filename;
return sRet;
}
std::string ExtractFileExt(const char* lpszFileName)
{
char _Drive[_MAX_DRIVE] = { 0 };
char _Dir[_MAX_DIR] = { 0 };
char _Filename[_MAX_FNAME] = { 0 };
char _Ext[_MAX_EXT] = { 0 };
_tsplitpath_s(lpszFileName, _Drive, _MAX_DRIVE, _Dir, _MAX_DIR, _Filename, _MAX_FNAME, _Ext, _MAX_EXT);
std::string sRet = _Ext;
return sRet;
}
搜索指定路径下所有文件
std::map<UINT, std::string> SearchFiles(const char* lpszFolder, const char* lpszExt, BOOL bIncludeSubFolder/* = FALSE*/)
{
std::map<UINT, std::string> result;
CFileFind FileFind;
CString strFolder; strFolder.Format(_T("%s*%s"), lpszFolder, lpszExt);
BOOL bFound = FileFind.FindFile(strFolder);
CString strNewFolder;
while (bFound)
{
bFound = FileFind.FindNextFile();
if (FileFind.IsDots())
continue;
if (FileFind.IsDirectory() && bIncludeSubFolder)
{
strFolder.Format(_T("%s%s\\"), lpszFolder, FileFind.GetFileName());
SearchFiles(strFolder, lpszExt, bIncludeSubFolder);
}
else
{
std::string filename = FileFind.GetFilePath();
std::string ext = ExtractFileExt(filename.c_str());
if (_tcsicmp(ext.c_str(), ".png")==0 ||
_tcsicmp(ext.c_str(), ".jpg") == 0)
result.insert(std::make_pair(result.size() + 1, filename));
}
}
FileFind.Close();
return result;
}