遍历一个目录下的文件,并把文件名保存在vector中

搜索一个文件目录及其下一级目录中某种类型的文件。把他们放进一个vector中。

#include <io.h>

#include <vector>
#include <AtlBase.h>
#include <time.h>
 
using namespace std;
void GetAllFileInfo(LPCTSTR path, vector<LPCTSTR> &filesPathVector)
{
    //找到第一个文件
    _tfinddata64_t c_file;
    intptr_t hFile;
    TCHAR root[MAX_PATH];
    _tcscpy(root,path);
    _tcscat(root,_T("\\*.*"));//查找带后缀的文件类型
    hFile=_tfindfirst64(root,&c_file);
    if( hFile  == -1)
        return;
 
    //search all files recursively.
    do
    {
        if(_tcslen(c_file.name)==1&&c_file.name[0]==_T('.')
            ||_tcslen(c_file.name)==2&&c_file.name[0]==_T('.')&&c_file.name[1]==_T('.'))
            continue;
        TCHAR *fullPath =new TCHAR[MAX_PATH];
        _tcscpy(fullPath,path);
        _tcscat(fullPath,_T("\\"));
        _tcscat(fullPath,c_file.name);
        if(c_file.attrib&_A_SUBDIR)
        {
            GetAllFileInfo(fullPath,filesPathVector);
        }
        else
        {
            //把文件全路径保存在vector中
            filesPathVector.push_back(fullPath);
            //print file info
            _tprintf(_T("FileName: %s\r\n"), fullPath);
            _tprintf(_T("ReadOnly: %s\r\n"), 
                ( c_file.attrib & _A_RDONLY ) ? _T(" Y ") : _T(" N ") );
            _tprintf(_T("Hidden:   %s\r\n"), 
                ( c_file.attrib & _A_HIDDEN ) ? _T(" Y ") : _T(" N ") );
            _tprintf(_T("System:   %s\r\n"), 
                ( c_file.attrib & _A_SYSTEM ) ? _T(" Y ") : _T(" N ") );
            _tprintf(_T("Arch:     %s\r\n"), 
                ( c_file.attrib & _A_ARCH )   ? _T(" Y ") : _T(" N ") );
            TCHAR timeBuffer[30];
            _tctime64_s( timeBuffer, _countof(timeBuffer), &c_file.time_write );
            _tprintf(_T("WriteTime:%.24s\r\n\r\n"),timeBuffer);
        }
    }
    while( _tfindnext64( hFile, &c_file ) == 0);
    //关闭搜索 handle
    _findclose(hFile);
}
 
int _tmain(int argc, _TCHAR* argv[])
{


    vector<LPCTSTR> filesPathVector;
    GetAllFileInfo(_T("E:\\IEtemp\\Internet 临时文件"),filesPathVector);
    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值