| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 | 
							- #include "pch.h"
 
- #include "findfile.h"
 
- #include <algorithm>
 
- // 查找文件的最大数量限制在nMaxLimit;
 
- CONST size_t nMaxLimit = 0xFFFFFFFF;	//--4294967295;
 
- // 查找文件的默认数量限制在nDefLimit;
 
- CONST size_t nDefLimit = 0xFFFFF;	//--1048575
 
- // 路径分隔符;
 
- CONST TCHAR		c_pathSeparator = _T('\\');
 
- CONST TString	s_pathSeparator = _T("\\");
 
- CONST TString	g_sVertical = _T("|");
 
- // 正确的扩展名及格式样列;
 
- // 这个值最好可以从配置文件ini或xml中获取,不用在代码里写死;
 
- CONST TString	g_sCorrectExt = _T("*.jpg|*.jpeg|*.png|*.bmp|*.cr2|*.nef|*.raw");
 
- findfile::findfile(void):
 
- m_nlimit(nDefLimit),
 
- m_pvtfiles(NULL),
 
- m_pvtnames(NULL),
 
- m_pvtfolders(NULL),
 
- m_pvtfiles_sth(NULL),
 
- m_pvtfiles_mth(NULL)
 
- {
 
- 	
 
- }
 
- findfile::~findfile(void)
 
- {
 
- }
 
- /************************************************************************/
 
- /*
 
- 函数:match
 
- 描述:判断指定的文件命名是否匹配指定的扩展名;
 
- 参数:
 
- IN	sFileExt	扩展名;
 
- IN	sFile		文件名;
 
- 返回:匹配返回非0值,否则返回0;
 
- 注意:
 
- */
 
- /************************************************************************/
 
- void findfile::setlimit(IN CONST INT &nLimit)
 
- {
 
- 	if (nLimit < 1)
 
- 		m_nlimit = nDefLimit;
 
- 	if (nLimit < nMaxLimit)
 
- 		m_nlimit = nLimit;
 
- }
 
- // 获取文件名;
 
- TString findfile::getfilename(IN CONST TString &file)
 
- {
 
- 	TString name;
 
- 	TString strfile = file;
 
- 	int nIndex = strfile.find_last_of(_T('\\'));	// 如果file不包含 '\\' ,得不到文件名;
 
- 	if (nIndex == TString::npos)
 
- 	{
 
- 		nIndex = strfile.find_last_of(_T('.'));
 
- 		if ( nIndex == TString::npos )
 
- 			return _T("");
 
- 		return strfile.substr(0, nIndex);
 
- 	}
 
- 	name = strfile.substr(nIndex+1);
 
- 	nIndex = name.find_last_of(_T('.'));
 
- 	if (nIndex == TString::npos)
 
- 		return _T("");
 
- 	return name.substr(0, nIndex);
 
- }
 
- void findfile::find1st(TString folder, TString &file)
 
- {
 
- 	TString path = folder;
 
- 	if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
 
- 		path.append(s_pathSeparator);
 
- 	TString _file = _T("*");
 
- 	TString s = path + _file;
 
- 	WIN32_FIND_DATA fileinfo = { 0 };
 
- 	HANDLE handle = FindFirstFile(s.c_str(), &fileinfo);
 
- 	if (NULL != handle && INVALID_HANDLE_VALUE != handle)
 
- 	{
 
- 		do
 
- 		{
 
- 			// '.'和 '..'的系统文件去除;
 
- 			if (_T('.') != fileinfo.cFileName[0])
 
- 			{
 
- 				if ((FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) != FILE_ATTRIBUTE_DIRECTORY)	// 目录;
 
- 				{
 
- 					file = path + fileinfo.cFileName;
 
- 					break;
 
- 				}
 
- 			}
 
- 		} while (FindNextFile(handle, &fileinfo));
 
- 		FindClose(handle);
 
- 	}
 
- }
 
- // 全部;
 
- void findfile::findall(IN CONST TString& folder)
 
- {
 
- 	TString path = folder;
 
- 	if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
 
- 		path.append(s_pathSeparator);
 
- 	TString file = _T("*");
 
- 	TString s = path + file;
 
- 	WIN32_FIND_DATA fileinfo = { 0 };
 
- 	HANDLE handle = FindFirstFile(s.c_str(), &fileinfo);
 
- 	if (NULL != handle && INVALID_HANDLE_VALUE != handle)
 
- 	{
 
- 		do
 
- 		{
 
- 			// 检查是否超过最大数;
 
- 			if (checklimit()) break;
 
- 			// '.'和 '..'的系统文件去除;
 
- 			if (_T('.') != fileinfo.cFileName[0])
 
- 			{
 
- 				if ((FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) == FILE_ATTRIBUTE_DIRECTORY)	// 目录;
 
- 				{
 
- 					m_pvtfolders->push_back(path + fileinfo.cFileName);
 
- 					findall(path + fileinfo.cFileName);
 
- 				}
 
- 				else
 
- 				{
 
- 					if (!checklimit())
 
- 					{
 
- 						m_pvtnames->push_back(fileinfo.cFileName);
 
- 						m_pvtfiles->push_back(path + fileinfo.cFileName);
 
- 					}
 
- 				}
 
- 			}
 
- 		} while (FindNextFile(handle, &fileinfo));
 
- 		FindClose(handle);
 
- 	}
 
- }
 
- void findfile::findsubfolder(IN CONST TString& folder)	// 查找子目录;
 
- {
 
- 	TString path = folder;
 
- 	if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
 
- 		path.append(s_pathSeparator);
 
- 	TString file = _T("*");
 
- 	TString s = path + file;
 
- 	WIN32_FIND_DATA fileinfo = { 0 };
 
- 	HANDLE handle = FindFirstFile(s.c_str(), &fileinfo);
 
- 	if (NULL != handle && INVALID_HANDLE_VALUE != handle)
 
- 	{
 
- 		do
 
- 		{
 
- 			if (_T('.') != fileinfo.cFileName[0])// '.'和 '..'的系统文件去除;
 
- 			{
 
- 				if ((FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) == FILE_ATTRIBUTE_DIRECTORY)	// 目录;
 
- 				{
 
- 					m_pvtfolders->push_back(path + fileinfo.cFileName);
 
- 				}
 
- 			}
 
- 		} while (FindNextFile(handle, &fileinfo));
 
- 		FindClose(handle);
 
- 	}
 
- }
 
- void findfile::findallsubfolder(IN CONST TString& folder)
 
- {
 
- 	TString path = folder;
 
- 	if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
 
- 		path.append(s_pathSeparator);
 
- 	TString file = _T("*");
 
- 	TString s = path + file;
 
- 	WIN32_FIND_DATA fileinfo = { 0 };
 
- 	HANDLE handle = FindFirstFile(s.c_str(), &fileinfo);
 
- 	if (NULL != handle && INVALID_HANDLE_VALUE != handle)
 
- 	{
 
- 		do
 
- 		{
 
- 			if (_T('.') != fileinfo.cFileName[0])// '.'和 '..'的系统文件去除;
 
- 			{
 
- 				if ((FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) == FILE_ATTRIBUTE_DIRECTORY)	// 目录;
 
- 				{
 
- 					m_pvtfolders->push_back(path + fileinfo.cFileName);
 
- 					findallsubfolder(path + fileinfo.cFileName); // 不入子目录查找;
 
- 				}
 
- 			}
 
- 		} while (FindNextFile(handle, &fileinfo));
 
- 		FindClose(handle);
 
- 	}
 
- }
 
- //////////////////////////////////////////////////////////////////////////
 
- void findfile::findfiles_findin_subfolder(IN CONST TString& folder)
 
- {
 
- 	TString path = folder;
 
- 	if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
 
- 		path.append(s_pathSeparator);
 
- 	TString file = _T("*");
 
- 	TString s = path + file;
 
- 	WIN32_FIND_DATA fileinfo = { 0 };
 
- 	HANDLE handle = FindFirstFile(s.c_str(), &fileinfo);
 
- 	if (NULL != handle && INVALID_HANDLE_VALUE != handle)
 
- 	{
 
- 		do
 
- 		{
 
- 			// 检查是否超过最大数;
 
- 			if (checklimit()) break;
 
- 			// '.'和 '..'的系统文件去除;
 
- 			if (_T('.') != fileinfo.cFileName[0])
 
- 			{
 
- 				if ((FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) == FILE_ATTRIBUTE_DIRECTORY)	// 目录;
 
- 				{
 
- 					findfiles_findin_subfolder(path + fileinfo.cFileName);
 
- 				}
 
- 				else
 
- 				{
 
- 					if (!checklimit())
 
- 					{
 
- 						m_pvtfiles->push_back(path + fileinfo.cFileName);
 
- 					}
 
- 				}
 
- 			}
 
- 		} while (FindNextFile(handle, &fileinfo));
 
- 		FindClose(handle);
 
- 	}
 
- }
 
- void findfile::findfiles_findout_subfolder(IN CONST TString& folder)
 
- {
 
- 	TString path = folder;
 
- 	if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
 
- 		path.append(s_pathSeparator);
 
- 	TString file = _T("*");
 
- 	TString s = path + file;
 
- 	WIN32_FIND_DATA fileinfo = { 0 };
 
- 	HANDLE handle = FindFirstFile(s.c_str(), &fileinfo);
 
- 	if (NULL != handle && INVALID_HANDLE_VALUE != handle)
 
- 	{
 
- 		do
 
- 		{
 
- 			// 检查是否超过最大数;
 
- 			if (checklimit()) break;
 
- 			// '.'和 '..'的系统文件去除;
 
- 			if (_T('.') != fileinfo.cFileName[0])
 
- 			{
 
- 				if ((FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) != FILE_ATTRIBUTE_DIRECTORY)	// 目录;
 
- 				{
 
- 					if (!checklimit())
 
- 					{
 
- 						m_pvtfiles->push_back(path + fileinfo.cFileName);
 
- 					}
 
- 				}
 
- 			}
 
- 		} while (FindNextFile(handle, &fileinfo));
 
- 		FindClose(handle);
 
- 	}
 
- }
 
- void findfile::findfiles_within_subfolder(IN CONST TString& folder)
 
- {
 
- 	TString path = folder;
 
- 	if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
 
- 		path.append(s_pathSeparator);
 
- 	TString file = _T("*");
 
- 	TString s = path + file;
 
- 	WIN32_FIND_DATA fileinfo = { 0 };
 
- 	HANDLE handle = FindFirstFile(s.c_str(), &fileinfo);
 
- 	if (NULL != handle && INVALID_HANDLE_VALUE != handle)
 
- 	{
 
- 		do
 
- 		{
 
- 			// 检查是否超过最大数;
 
- 			if (checklimit()) break;
 
- 			// '.'和 '..'的系统文件去除;
 
- 			if (_T('.') != fileinfo.cFileName[0])
 
- 			{
 
- 				if ((FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) == FILE_ATTRIBUTE_DIRECTORY)	// 目录;
 
- 				{
 
- 					m_pvtfolders->push_back(path + fileinfo.cFileName);
 
- 					findfiles_within_subfolder(path + fileinfo.cFileName);
 
- 				}
 
- 				else
 
- 				{
 
- 					if (!checklimit())
 
- 					{
 
- 						m_pvtfiles->push_back(path + fileinfo.cFileName);
 
- 					}
 
- 				}
 
- 			}
 
- 		} while (FindNextFile(handle, &fileinfo));
 
- 		FindClose(handle);
 
- 	}
 
- }
 
 
  |