如何遍历目录及其子目录(自己参考着改改吧) void FindDirFiles(CString csDirPath, ThreadInfo *pThreadInfo) { WIN32_FIND_DATA wfd; HANDLE hFind; CString csText = _T(""); // Check if the last char is a back-slash // (If not, put it there) if (csDirPath.Right(1) != "\\") csDirPath += _T("\\"); // set the variable and add an astrix for // the beginning of the directory search. csText = csDirPath + _T("*"); // Iterate through dirs hFind = FindFirstFile(csText, &wfd); if (hFind != INVALID_HANDLE_VALUE) { do {
// Check if "." or "..", if not... // Check if its a directory. if ((strcmp(wfd.cFileName,_T("."))) && (strcmp(wfd.cFileName,_T(".."))) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { CString csDirIn = _T(""); // Set to the directory found. csDirIn = csDirPath + wfd.cFileName; // Call this function again. FindDirFiles(csDirIn, pThreadInfo); } } while (FindNextFile(hFind, &wfd));
// This is a MUST FindClose(hFind); } // Iterate through files // // set the variable and add an astrix-dot-astrix "*.*" // for the beginning of the file search. csText = csDirPath + _T("*.*"); hFind = FindFirstFile(csText, &wfd); if (hFind != INVALID_HANDLE_VALUE) { do {
// If NOT a directory... if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) { CString csIn = _T(""); CString csOut = _T(""); // Set to the file found. csIn = csDirPath + wfd.cFileName;
if (!pThreadInfo->bEncrypt) { csOut = csIn.Left(csIn.GetLength() - 4); } else { csOut = csIn + ".enf"; } // Now, add the filename into the Array. m_csFileArrayIn.Add(csIn); m_csFileArrayOut.Add(csOut); } } while (FindNextFile(hFind, &wfd)); // This is a MUST FindClose(hFind); } }
finder.FindFile("*.*");
while(finder.FindNextFile()){
if(finder.IsDirectory()){//目录
if(finder.GetFileName()!="." && finder.GetFileName()!="..")
finder.GetFileName();//取得子目录这里可以放一个递归什么的,继续取得子目录下面的文件夹和文件
}
finder.GetFileName();//取得文件
}
CString str; str="C:\\*.*"; BOOL bWorking = finder.FindFile(str);
while (bWorking)
{
bWorking = finder.FindNextFile();
strcpy(pszText,finder.GetFileTitle());
} finder.Close();
这句是取到了返回跟或上一级目录的文件夹,可以去掉,我只是这么写着,具体自己写,很简单的
BOOL GetFolderFile(CString pPath)
{
CFileFind finder;
int i = 0;
BOOL bWorking = finder.FindFile(pPath + "\\*");
while (bWorking)
{
bWorking = finder.FindNextFile();
if (!finder.IsDirectory())
{
CString pName = finder.GetFileName();
CTime tempTime;
CString pSize, pDate;
DWORD pLength = finder.GetLength();
DWORD tmp; tmp = pLength / 1048576;
if (tmp > 0)
{
pSize.Format("%d MB", tmp);
}
else
{
tmp = pLength / 1024;
if (tmp > 0)
{
pSize.Format("%d KB", tmp);
}
else
{
pSize.Format("%d byte", tmp);
}
}
if (finder.GetCreationTime(tempTime))
{
pDate = tempTime.Format(_T("%Y-%m-%d- %H:%M:%S"));
}
else
pDate = _T("(unavailable)"); CString pPath = finder.GetFilePath();
m_pView->AddItem(i, pName, pSize, pDate, pPath);
i ++;
}
}
return TRUE;
}
void FindDirFiles(CString csDirPath, ThreadInfo *pThreadInfo)
{
WIN32_FIND_DATA wfd;
HANDLE hFind;
CString csText = _T(""); // Check if the last char is a back-slash
// (If not, put it there)
if (csDirPath.Right(1) != "\\")
csDirPath += _T("\\"); // set the variable and add an astrix for
// the beginning of the directory search.
csText = csDirPath + _T("*"); // Iterate through dirs
hFind = FindFirstFile(csText, &wfd);
if (hFind != INVALID_HANDLE_VALUE) {
do {
// Check if "." or "..", if not...
// Check if its a directory.
if ((strcmp(wfd.cFileName,_T("."))) && (strcmp(wfd.cFileName,_T(".."))) &&
(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { CString csDirIn = _T(""); // Set to the directory found.
csDirIn = csDirPath + wfd.cFileName; // Call this function again.
FindDirFiles(csDirIn, pThreadInfo);
}
} while (FindNextFile(hFind, &wfd));
// This is a MUST
FindClose(hFind);
} // Iterate through files
//
// set the variable and add an astrix-dot-astrix "*.*"
// for the beginning of the file search.
csText = csDirPath + _T("*.*");
hFind = FindFirstFile(csText, &wfd);
if (hFind != INVALID_HANDLE_VALUE) {
do {
// If NOT a directory...
if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) { CString csIn = _T("");
CString csOut = _T(""); // Set to the file found.
csIn = csDirPath + wfd.cFileName;
if (!pThreadInfo->bEncrypt) {
csOut = csIn.Left(csIn.GetLength() - 4);
}
else {
csOut = csIn + ".enf";
} // Now, add the filename into the Array.
m_csFileArrayIn.Add(csIn);
m_csFileArrayOut.Add(csOut);
}
} while (FindNextFile(hFind, &wfd)); // This is a MUST
FindClose(hFind); }
}
在全盘查找时,其实有三种可能。你有可能遇到的是,目录、文件、还有根目录"."
和上级目录".."
判断是否是上级目录,或是根目录。可用成员函数。finder.isdot()进行