//************************************
// Method: 深度优先法实现扫描目录, 使用递归方式
// FullName: CScanDirectoryDlg::ScanDirectoryByDeep
// Access: public static
// Returns: int 返回历时
// Parameter: CString& csTextOut [out]
// Parameter: LPCTSTR pszRoot
//************************************
unsigned long ScanDirectoryByDeep( CString& csTextOut, LPCTSTR pszRoot, int nDeep )
{
CString csFileList;
CFileFind finder;
unsigned long nTimeSpan = 0;
if (0 == nDeep)
{
nTimeSpan = GetCurTimeStamp();
}
// build a string with wildcards
CString csDir(pszRoot);
// start working for files
BOOL bWorking = finder.FindFile(csDir + _T("\\*.*"));
//遍历csDir目录下的文件和子目录
while (bWorking)
{
bWorking = finder.FindNextFile();
// skip . and .. files; otherwise, we'd
// recur infinitely!
if (finder.IsDots())
{
continue;
}
CString csFileOrDir = finder.GetFilePath();
// if it's a directory
if (finder.IsDirectory())
{
ScanDirectoryByDeep(csTextOut, csFileOrDir, nDeep + 1);
csTextOut.AppendFormat(_T("[%u-D]%s\r\n"), nDeep, csFileOrDir);
}
else //it's a file
{
csFileList.AppendFormat(_T("[%u-F]%s\r\n"), nDeep, csFileOrDir);
}
}
finder.Close();
//将文件名附进来
csTextOut += csFileList;
//根层进入时统计时间
if (0 == nDeep)
{
nTimeSpan = GetCurTimeStamp() - nTimeSpan;
}
return nTimeSpan;
}
//************************************
// Method: 实现广度扫描
// FullName: CScanDirectoryDlg::ScanDirectoryByBreadth
// Access: public static
// Returns: unsigned long 返回历时
// Parameter: CString & csTextOut
// Parameter: LPCTSTR pszRoot
// Parameter: int nDeep 层次 (目录层次输出暂未实现)
//************************************
unsigned long ScanDirectoryByBreadth( CString& csTextOut, LPCTSTR pszRoot, int nDeep )
{
// 搜索该路径下的文件
deque<CString> deDirectory;
CFileFind finder;
BOOL bWorking;
unsigned long nTimeSpan;
nTimeSpan = GetCurTimeStamp(); //time count start
CString csDir(pszRoot);
nDeep = 0;
bWorking = finder.FindFile(csDir + _T("\\*.*"));
while (bWorking)
{
bWorking = finder.FindNextFile();
// skip . and .. files;
if (finder.IsDots())
{
continue;
}
// if it's a directory
CString strSubDir = finder.GetFilePath();
if (finder.IsDirectory())
{
csTextOut.AppendFormat(_T("[%u-D]%s\r\n"), nDeep, strSubDir);//记录下文件目录名
deDirectory.push_back(strSubDir);
}
else //it's a file
{
csTextOut.AppendFormat(_T("[%u-F]%s\r\n"), nDeep, strSubDir); // 记录下文件名
}
}
// 寻找一级目录下的子目录或文件
++nDeep;
while(!deDirectory.empty())
{
std::deque<CString>::iterator it = deDirectory.begin();
CFileFind finder;
BOOL bWorking = finder.FindFile((*it) + _T("\\*.*"));
while(bWorking)
{
bWorking = finder.FindNextFile();
if (finder.IsDots())
{
continue;
}
// if it's a directory
CString csFileOrDir = finder.GetFilePath();
if (finder.IsDirectory())
{
csTextOut.AppendFormat(_T("[%u-D]%s\r\n"), nDeep, csFileOrDir); //记录下文件目录名
deDirectory.push_back(csFileOrDir);
}
else //it's a file
{
csTextOut.AppendFormat(_T("[%u-D]%s\r\n"), nDeep, csFileOrDir); // 记录下文件名
}
}
// 删除队列的头元素
deDirectory.pop_front();
}
nTimeSpan = GetCurTimeStamp() - nTimeSpan; //time count end
return nTimeSpan;
}
// Method: 深度优先法实现扫描目录, 使用递归方式
// FullName: CScanDirectoryDlg::ScanDirectoryByDeep
// Access: public static
// Returns: int 返回历时
// Parameter: CString& csTextOut [out]
// Parameter: LPCTSTR pszRoot
//************************************
unsigned long ScanDirectoryByDeep( CString& csTextOut, LPCTSTR pszRoot, int nDeep )
{
CString csFileList;
CFileFind finder;
unsigned long nTimeSpan = 0;
if (0 == nDeep)
{
nTimeSpan = GetCurTimeStamp();
}
// build a string with wildcards
CString csDir(pszRoot);
// start working for files
BOOL bWorking = finder.FindFile(csDir + _T("\\*.*"));
//遍历csDir目录下的文件和子目录
while (bWorking)
{
bWorking = finder.FindNextFile();
// skip . and .. files; otherwise, we'd
// recur infinitely!
if (finder.IsDots())
{
continue;
}
CString csFileOrDir = finder.GetFilePath();
// if it's a directory
if (finder.IsDirectory())
{
ScanDirectoryByDeep(csTextOut, csFileOrDir, nDeep + 1);
csTextOut.AppendFormat(_T("[%u-D]%s\r\n"), nDeep, csFileOrDir);
}
else //it's a file
{
csFileList.AppendFormat(_T("[%u-F]%s\r\n"), nDeep, csFileOrDir);
}
}
finder.Close();
//将文件名附进来
csTextOut += csFileList;
//根层进入时统计时间
if (0 == nDeep)
{
nTimeSpan = GetCurTimeStamp() - nTimeSpan;
}
return nTimeSpan;
}
//************************************
// Method: 实现广度扫描
// FullName: CScanDirectoryDlg::ScanDirectoryByBreadth
// Access: public static
// Returns: unsigned long 返回历时
// Parameter: CString & csTextOut
// Parameter: LPCTSTR pszRoot
// Parameter: int nDeep 层次 (目录层次输出暂未实现)
//************************************
unsigned long ScanDirectoryByBreadth( CString& csTextOut, LPCTSTR pszRoot, int nDeep )
{
// 搜索该路径下的文件
deque<CString> deDirectory;
CFileFind finder;
BOOL bWorking;
unsigned long nTimeSpan;
nTimeSpan = GetCurTimeStamp(); //time count start
CString csDir(pszRoot);
nDeep = 0;
bWorking = finder.FindFile(csDir + _T("\\*.*"));
while (bWorking)
{
bWorking = finder.FindNextFile();
// skip . and .. files;
if (finder.IsDots())
{
continue;
}
// if it's a directory
CString strSubDir = finder.GetFilePath();
if (finder.IsDirectory())
{
csTextOut.AppendFormat(_T("[%u-D]%s\r\n"), nDeep, strSubDir);//记录下文件目录名
deDirectory.push_back(strSubDir);
}
else //it's a file
{
csTextOut.AppendFormat(_T("[%u-F]%s\r\n"), nDeep, strSubDir); // 记录下文件名
}
}
// 寻找一级目录下的子目录或文件
++nDeep;
while(!deDirectory.empty())
{
std::deque<CString>::iterator it = deDirectory.begin();
CFileFind finder;
BOOL bWorking = finder.FindFile((*it) + _T("\\*.*"));
while(bWorking)
{
bWorking = finder.FindNextFile();
if (finder.IsDots())
{
continue;
}
// if it's a directory
CString csFileOrDir = finder.GetFilePath();
if (finder.IsDirectory())
{
csTextOut.AppendFormat(_T("[%u-D]%s\r\n"), nDeep, csFileOrDir); //记录下文件目录名
deDirectory.push_back(csFileOrDir);
}
else //it's a file
{
csTextOut.AppendFormat(_T("[%u-D]%s\r\n"), nDeep, csFileOrDir); // 记录下文件名
}
}
// 删除队列的头元素
deDirectory.pop_front();
}
nTimeSpan = GetCurTimeStamp() - nTimeSpan; //time count end
return nTimeSpan;
}