////////////////////////////////////////////////////////////////////// // // FileFinder.cpp: implementation of the myFind class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "myfind.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// myFind::myFind() { m_bStopSearch = false; m_pFileFinderProc = NULL; m_pCustomParam = NULL; } myFind::~myFind() { } int myFind::FindFiles(LPCTSTR szBaseFolder, LPCTSTR szFileMask, BOOL bSubFolders) { CFindOpts opts; opts.sBaseFolder = szBaseFolder; opts.sFileMask = szFileMask; opts.bSubfolders = bSubFolders; // Get all files, but no directories opts.FindAllFiles(); Find(opts); return GetFileCount(); } //查找指定初始目录下符合条件的文件 int myFind::Find(myFind::CFindOpts &opts) { CFileFind finder; CString sFullMask; CFindOpts subOpts; BOOL bFound, bValidFile; CTime timeFile; m_bStopSearch = false; opts.sBaseFolder = myPath::AddBackSlash(opts.sBaseFolder);//添加"/" // 是否需要查找子目录 if (opts.bSubfolders) { sFullMask = opts.sBaseFolder + CString("*.*"); bFound = finder.FindFile(sFullMask); while ((bFound) && (!m_bStopSearch)) { bFound = finder.FindNextFile(); if ((finder.IsDirectory()) && (!finder.IsDots())) //查找目录 { subOpts = opts; subOpts.sBaseFolder = opts.sBaseFolder + finder.GetFileName(); //递归查找子目录 Find(subOpts); } } } m_sSearchingFolder = opts.sBaseFolder;//正在查找的目录 // 调用回调函数 if (m_pFileFinderProc != NULL) m_pFileFinderProc(this, FF_FOLDER, m_pCustomParam); //找到了目录,利用回调显示目录 sFullMask = opts.sBaseFolder + opts.sFileMask; bFound = finder.FindFile(sFullMask); while ((bFound) && (!m_bStopSearch)) { bFound = finder.FindNextFile(); if (!finder.IsDots()) { // 检查限制 bValidFile = TRUE; if (opts.dwOptionsFlags & FIND_ATTRIBUTES) { bValidFile = finder.MatchesMask(opts.dwFileAttributes); } // 如果符合所有的查找条件 if (bValidFile) { CString sName = finder.GetFilePath(); if (finder.IsDirectory()) sName += "\\"; m_aFilesFound.Add(sName);//添加找到的路径 } // 调用回调函数 if (m_pFileFinderProc != NULL) m_pFileFinderProc(this, bValidFile ? FF_FOUND : FF_DISCARDED, m_pCustomParam);//文件找到或没找到的状态显示 } } finder.Close(); return GetFileCount();//返回找到的文件数目 } int myFind::GetFileCount() { return m_aFilesFound.GetSize(); } int myFind::FindPathItem(LPCTSTR szPath) { bool bFound; int nIndex; for (nIndex = 0; nIndex < m_aFilesFound.GetSize(); nIndex++) { bFound = (m_aFilesFound[nIndex].CompareNoCase(szPath) == 0); if (bFound) break; } return (bFound ? nIndex : -1); } myPath myFind::GetFilePath(int nIndex) { if ((nIndex < 0) || (nIndex >= GetFileCount())) return ""; return m_aFilesFound[nIndex]; } LPCTSTR myFind::GetSearchingFolder() { return m_sSearchingFolder; } void myFind::RemoveAt(int nIndex) { if ((nIndex < 0) || (nIndex >= GetFileCount())) return; m_aFilesFound.RemoveAt(nIndex); } void myFind::RemoveAll() { if (GetFileCount() > 0) m_aFilesFound.RemoveAll(); } void myFind::SetCallback(FILEFINDERPROC pFileFinderProc, void *pCustomParam) { m_pFileFinderProc = pFileFinderProc; m_pCustomParam = pCustomParam; } void myFind::StopSearch() { m_bStopSearch = true; }