1:递归搜索
typedef struct tagFILELIST
{
BYTE nFileType;
WCHAR wchFileName[MAX_PATH];
WCHAR wchFilePath[MAX_PATH];
}FILEINFO, *PFILEINFO;
void GetAllFileInfo(WCHAR *pFilePath)
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
FILEINFO fileInfo;
CString Templujing=pFilePath;
//RETAILMSG(TRUE, (TEXT("YYYYYYYYY=====%s\r\n"),Templujing));
WCHAR *pFilter = L"\\*.*";
WCHAR wchFilePath[MAX_PATH];
memset(wchFilePath, 0, MAX_PATH);
wcscpy(wchFilePath, pFilePath);
wcscat(wchFilePath, pFilter);
ZeroMemory(&fileInfo, sizeof(fileInfo));
//m_FileListArr.RemoveAll();
hFind = FindFirstFile(wchFilePath, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
return;
}
do
{
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
//RETAILMSG(1, (_T("FileFile_%s \r\n"), FindFileData.cFileName));
fileInfo.nFileType = 0;
wcscpy(fileInfo.wchFileName, FindFileData.cFileName);
wcscpy(fileInfo.wchFilePath, pFilePath);
wcscat(fileInfo.wchFilePath, _T("\\"));
wcscat(fileInfo.wchFilePath, FindFileData.cFileName);
//m_FileListArr.Add(fileInfo);
GetAllFileInfo(fileInfo.wchFilePath);
}
else
{
//RETAILMSG(1, (_T("FileFile__999 %s \r\n"), FindFileData.cFileName));
CString strTemp = FindFileData.cFileName, strsuffix;
//RETAILMSG(TRUE, (TEXT("strTemp87=====%s\r\n"),strTemp));
strsuffix = strTemp.Mid(strTemp.ReverseFind(L'.' )+1); //文件的后缀名
//RETAILMSG(TRUE, (TEXT("strsuffix=====%s\r\n"),strsuffix));
////if(TestFile(FindFileData.cFileName))
//{
// fileInfo.nFileType = 1;
// wcscpy(fileInfo.wchFileName, FindFileData.cFileName);
// wcscpy(fileInfo.wchFilePath, pFilePath);
// wcscat(fileInfo.wchFilePath, _T("\\"));
// wcscat(fileInfo.wchFilePath, FindFileData.cFileName);
// //m_FileListArr.Add(fileInfo);
//}
// if (wcscmp(strsuffix, L"mp3" ) == 0 || wcscmp(strsuffix, L"wav" ) == 0 || wcscmp(strsuffix, L"wma" ) ==hOTv1
if (
wcsicmp(strsuffix, L"AVI" ) == 0
|| wcsicmp(strsuffix, L"WMV" ) == 0
|| wcsicmp(strsuffix, L"MPG" ) == 0
|| wcsicmp(strsuffix, L"MP4" ) == 0
|| wcsicmp(strsuffix, L"RMVB" ) == 0
|| wcsicmp(strsuffix, L"TS" ) == 0
|| wcsicmp(strsuffix, L"M2TS" ) == 0
|| wcsicmp(strsuffix, L"TP" ) == 0
|| wcsicmp(strsuffix, L"MOV" ) == 0
|| wcsicmp(strsuffix, L"VOB" ) == 0
|| wcsicmp(strsuffix, L"MKV" ) == 0
|| wcsicmp(strsuffix, L"3GP" ) == 0
|| wcsicmp(strsuffix, L"3GA" ) == 0
|| wcsicmp(strsuffix, L"ASF" ) == 0
|| wcsicmp(strsuffix, L"DIVX" ) == 0
|| wcsicmp(strsuffix, L"TRP" ) == 0
|| wcsicmp(strsuffix, L"FLV" ) == 0
|| wcsicmp(strsuffix, L"RM" ) == 0
|| wcsicmp(strsuffix, L"MPEG" ) == 0
// || wcsicmp(strsuffix, L"DAT" ) == 0
|| wcsicmp(strsuffix, L"DIVX" ) == 0
|| wcsicmp(strsuffix, L"M4V" ) == 0
|| wcsicmp(strsuffix, L"M2V" ) == 0
)
{
CString strs=Templujing /*= Str*/;
strs += L"\\" ;
strs += FindFileData.cFileName;
//RETAILMSG(TRUE, (TEXT("xxxxxxxxxxxxxxxxxxxxxxxxx======%s\r\n"),strs));
if (nTotalFileCount<MAXNUM)
{
ListItem item={0};
item.Text=get_all_path_by_filename(strs,0);
RETAILMSG(TRUE, (TEXT("item.Text======%s\r\n"),item.Text));
Mybofangqi_filename.push_back(strs);
item.Height=52 ;
m_ChildList.AddItem(item);
// //2012.2.24RETAILMSG(TRUE, (TEXT("HMJ FILENAME======%s\r\n"),strs));
//把文件保存起来
/* PathAndFileName[nTotalFileCount]=strs; */
nTotalFileCount++;
}
else
{ //
// //2012.2.24RETAILMSG(1, (L"==GetAllFileInfo===4444=\r\n"));
// AfxMessageBox(TEXT("内存有限请不要再添加" ));
break ;
}
}
}
}while(FindNextFile(hFind,&FindFileData));
::FindClose(hFind);
}
2:DFS(深度优先搜索,Depth-first search),所以需要自己模拟堆栈,而Ramond Chen 大虾说 breadth-first searching is better for file system tree walking,也就是说对于文件系统树的遍历来说使用广度优先要比深度优先要更好些,理由是:1、DFS每次遇到目录则立即深入搜索,因此需要保存大量句柄,而BFS则避免用太多handle;2、同一个目录下的文件在硬盘的物理结构上更邻近些,所以BFS更快。所以决定更新一些code,增加了BFS的实现,原来的递归以及DFS的代码还在,不过都被我#ifdef掉了,默认是BFS,有需要研究的可以在 FileEnumerator.h 里找到下述代码自行调整测试:
代码后续。。。
.......................................
然后就是用它们做实事了:如果是要实现DFS,那么由于每次深入遍历子目录的时候都必须保留当前所有已遍历的搜索句柄等信息,其后还要再在回溯的时候继续上一层目录的搜索工作,所以会要用到栈。由于现在是实现广度优先,当遇到一个目录的时候并不用急着深入搜索,而是先把相关信息给记录到一个队列里面,等当前目录的所有文件都访问完后,再pop队列的头(也就是下一个待遍历的目录)继续遍历,从而实现所谓的BFS。
代码后续。。。
..............................