方法一
system("dir /b /a-d /os c:\\*.* >d:\\allfiles.txt");
//读文件d:\\allfiles.txt的内容即C:\\下所有文件的名字,按文件大小从小到大顺序
system("dir /b /a-d /s /o-d c:\\*.* >d:\\allfilesinsub.txt");
//读文件d:\\allfilesinsub.txt的内容即C:\\下所有文件的名字包含子目录,按文件时间从新到旧顺序
system("dir /b /ad c:\\*.* >d:\\alldirs.txt");
//读文件d:\\alldirs.txt的内容即C:\\下所有子目录的名字
请记住,能用shell命令获取文件、文件夹信息或者操作文件、文件夹最好用shell命令获取或者操作,而不要用各种API获取或者操作,因为当遇到非法文件夹名或非法文件名或非法文件长度、非法文件日期、压缩文件、链接文件、稀疏文件……等各种意料之外的情况时,API会处理的不全面或陷入死循环,而shell命令不会。
方法二
//集合
#include <vector>
#define VEC_STRING std::vector<CString>
VEC_STRING VecImageName;//图片文件夹的图片名称集合
GetFloderVecImageName(szImageFloderPath, VecImageName);//获取图片名称集合
sort(VecImageName.begin(), VecImageName.end(), CmpImageName);
/***********************************************************
** 功能描述: 获取文件夹下的图片名称集合
************************************************************/
void CReadImageDataDlg::GetFloderVecImageName(CString szPath, VEC_STRING& VecImageName)
{
CFileFind ff;
BOOL bFind = ff.FindFile(szPath + _T("\\*.*"));
while(bFind)
{
bFind = ff.FindNextFile();
if (ff.IsDots()
|| ff.IsSystem()
|| ff.IsHidden()
|| ff.IsDirectory())
{
continue;
}
else
{
CString szFilePath = ff.GetFilePath();
CString szExt = HandlePath::GetFileExt(szFilePath);//文件后缀
//判断文件后缀
if (_T("bmp") != szExt)
{
continue;
}
CString szFileName = ff.GetFileName();
//判断文件名称
//if (szFileName.Left(4) != _T("IMG_"))
//{
// continue;
//}
//添加图片名称
VecImageName.push_back(szFileName);
}
}
}
//用来做图片名称比较函数
bool CmpImageName(CString &string1, CString &string2)
{
CString szNum1 = string1.Mid(4, string1.GetLength() - 8);
int nNum1 = _ttoi(szNum1);
CString szNum2 = string2.Mid(4, string2.GetLength() - 8);
int nNum2 = _ttoi(szNum2);
if (nNum1 < nNum2)
{
return true;
}
else
{
return false;
}
}