// 获取指定文件夹下的所有文件列表
void GetFilePath(CString sPath, CStringList *pstrNameList);
// ------------------------------------------------------------------
// 获取指定文件夹下的所有文件列表
// add by jzh 2009-07-06
// ------------------------------------------------------------------
void CommFunc::GetFilePath(CString sPath, CStringList *psNameList)
{
CFileFind ff; //定义一个CfileFind对象
//BOOL bFlag;
sPath = sPath + "*.*";
if (!ff.FindFile(sPath)) //打开以sPath为目标的文件查询,并检查调用是否成功
{
return;
}
else
{
// FindNextFile()的注意漏掉最后一项
while (ff.FindNextFile()) //查找下个文件
{
//if (ff.IsDots())
//continue;
if (!ff.IsDots() && ff.IsDirectory())
{
CString sTemp;
int iIndex;
sTemp = ff.GetFilePath(); //取到目录的全路径
if (sTemp.Right(1) != "\\")
sTemp += "\\";
iIndex = sPath.ReverseFind('\\');
sTemp += sPath.Right(sPath.GetLength() - iIndex - 1); //生成路径
GetFilePath(sTemp, psNameList); //递归调用函数
}
else if(!ff.IsDots() && !ff.IsDirectory())
{
psNameList->AddTail(ff.GetFilePath()); //加入文件列表
//MessageBox(NULL, ff.GetFilePath(), TEXT("提示"), MB_OK);
}
}
// 当得到最后一个文件时再进行FindNextFile()必然返回0
// psNameList里面无法获得最后一项文件名
// 所以再判断一次
if (!ff.IsDots() && ff.IsDirectory())
{
CString sTemp;
int iIndex;
sTemp = ff.GetFilePath(); //取到目录的全路径
if (sTemp.Right(1) != "\\")
sTemp += "\\";
iIndex = sPath.ReverseFind('\\');
sTemp += sPath.Right(sPath.GetLength() - iIndex - 1); //生成路径
GetFilePath(sTemp, psNameList); //递归调用函数
}
else if(!ff.IsDots() && !ff.IsDirectory())
{
psNameList->AddTail(ff.GetFilePath()); //加入文件列表
//MessageBox(NULL, ff.GetFilePath(), TEXT("提示"), MB_OK);
}
}
ff.Close();
}