获取文件夹下所有文件 ,并排序

方法一

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; 
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值