//遍历所有文件CString strFoldername
void CCollectDlg::FindFiles()
{
int nAllRows = 0;
CString fileExtensions = "所有文件(*.h;*.cpp) |*.h;*.cpp||";
CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT, fileExtensions, this);
const int MIN_FILE_NUMBER = 10; //至少允许选择5个文件
dlg.m_ofn.lpstrFile = new TCHAR[_MAX_PATH * MIN_FILE_NUMBER]; //重新定义缓冲区大小
memset(dlg.m_ofn.lpstrFile, 0, _MAX_PATH * MIN_FILE_NUMBER); //初始化定义的缓冲区
dlg.m_ofn.nMaxFile = _MAX_PATH * MIN_FILE_NUMBER;
int nFiles = 0;
CString strFilePath;
CString strFileName;
if(dlg.DoModal() == IDOK)
{
POSITION posFile=dlg.GetStartPosition();
while(posFile!=NULL)
{
strFilePath = dlg.GetNextPathName(posFile);
strFileName = dlg.GetFileName();
if (AllRows(strFilePath) <= 0)
{//处理出错,直接退出
delete[] dlg.m_ofn.lpstrFile;
MessageBox(strFilePath);
return ;
}
nAllRows += AllRows(strFilePath);
//GetDlgItem(IDC_STATIC_PATH)->SetWindowText("正在处理"+strFileName);
nFiles++;
}
}
else
{
delete[] dlg.m_ofn.lpstrFile; //最后别忘了释放内存
return ;
}
delete[] dlg.m_ofn.lpstrFile; //最后别忘了释放内存
strFileName.Format("处理完成,共处理 %d 个文件\n\n总行数为: %d ", nFiles, nAllRows);
MessageBox(strFileName);
return;
}
int CCollectDlg::AllRows(CString strFilePath)
{
CStdioFile m_sfile;
if (!m_sfile.Open(strFilePath, CFile::modeReadWrite | CFile::typeText))
{
MessageBox("Open file1 error!");
return FALSE;
}
CString strData;
int nRow = 1;
while(m_sfile.ReadString(strData))
{
nRow++;
}
m_sfile.Close();
return nRow;
}