有位高手给了遍历文件的代码非常好:
void Ctest3Dlg::ScanDir(CString strPath)
{
CStringArray strPathArr;
strPathArr.Add(strPath); while(strPathArr.GetSize() > 0)
{
CFileFind oFileFind;
CString strDir = strPathArr.GetAt(0);
strPathArr.RemoveAt(0);//移除掉此目录路径 if(strDir.Right(1) != "\\")
strDir += "\\*.*";
else
strDir += "*.*"; bool bFind = oFileFind.FindFile(strDir);
while(bFind)
{ bFind = oFileFind.FindNextFile(); if(oFileFind.IsDots())
continue; if(oFileFind.IsDirectory())//文件夹
{
CString strPath = oFileFind.GetFilePath();
strPathArr.Add(strPath);//加入这个新发现的子目录
Out2List(strPath);//这里可以输出你的新发现的目录
}
else//下面发现的是文件
{
CString strFileName;
strFileName = oFileFind.GetFileName();
Out2List(strPath);//这里可以输出新发现的文件名
}
}
}
}
所有文件可以遍历,并且我将他显示了出来,我现在的问题是,我想用一个进度条来显示进度
可是,我怎么获取遍历文件的时间呢?也就是说用进度条来显示进度该如何实现呢?谢谢
void Ctest3Dlg::ScanDir(CString strPath)
{
CStringArray strPathArr;
strPathArr.Add(strPath); while(strPathArr.GetSize() > 0)
{
CFileFind oFileFind;
CString strDir = strPathArr.GetAt(0);
strPathArr.RemoveAt(0);//移除掉此目录路径 if(strDir.Right(1) != "\\")
strDir += "\\*.*";
else
strDir += "*.*"; bool bFind = oFileFind.FindFile(strDir);
while(bFind)
{ bFind = oFileFind.FindNextFile(); if(oFileFind.IsDots())
continue; if(oFileFind.IsDirectory())//文件夹
{
CString strPath = oFileFind.GetFilePath();
strPathArr.Add(strPath);//加入这个新发现的子目录
Out2List(strPath);//这里可以输出你的新发现的目录
}
else//下面发现的是文件
{
CString strFileName;
strFileName = oFileFind.GetFileName();
Out2List(strPath);//这里可以输出新发现的文件名
}
}
}
}
所有文件可以遍历,并且我将他显示了出来,我现在的问题是,我想用一个进度条来显示进度
可是,我怎么获取遍历文件的时间呢?也就是说用进度条来显示进度该如何实现呢?谢谢
如果是遍历是更为复杂的操作,那么可以先预先遍历一次,统计出数量,并把文件名存放到Vector中。
然后再执行更为复杂的操作,然后是滚动条~
2)创建一个线程用于显示进度条。
3)根据vector变量的长度循环处理你想要操作的事情。文件个数超过总个数的1%就给线程发消息。
线程:获得消息并更新进度条。
主线程:处理文件的相关操作。
你可以通过CFileFind的GetLength( ) 得到文件大小,通过文件大小来设置进度走向,
这样做进度虽然和真正遍历的进度不是很准确,不过还是能把想要做的都表示出来,而且做起来也比较方便