获取指定目录下(包括子目录)的指定后缀的文件

该博客介绍了如何使用C++编写一个函数`FindMyFile`,该函数能够遍历指定目录及其子目录,查找并收集所有具有特定后缀的文件路径。通过`CFileFind`类进行文件查找,并将匹配的文件路径存储在字符串数组中。示例代码展示了如何调用这个函数来查找`.txt`文件并显示结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

获取指定目录下(包括子目录)的指定后缀的文件
#include <DIRECT.H>
/********************************************************/
/* Syntax:
/*         void FindMyFile(CString strPath, CString strSuffix, CStringArray& arrPath)  
/* Remarks:
/*        Find files with specified suffix in specified directory.
/* Return Values:
/*        None.
/* Parameters:
/* strPath: 
/*        Directory for search.
/* strSufffix:
/*        File Suffix.
/* arrPath:
/*        A array used to store the full Path of file.
/* Author:
/*        lixiaosan
/* Create Date:
/*        April 07 2006
/********************************************************/
void CTest6Dlg::FindMyFile(CString strPath, 
                           CString strSuffix, 
                           CStringArray& arrPath) 

    BOOL bFind, bFindSuffix; 
    CFileFind tempFind, tempFind1; 
     
    _chdir(strPath); 
    bFind = tempFind.FindFile(_T("*.*")); 
     
    while ( bFind ) 
    { 
        bFind = tempFind.FindNextFile(); 
        if (tempFind.IsDirectory()) 
        { 
            if ( !tempFind.IsDots() ) 
            { 
                CString strTempPath; 
                strTempPath = tempFind.GetFilePath(); 
                FindMyFile(strTempPath);  
            } 
        } 
    } 
     
    _chdir(strPath); 
    bFindSuffix = tempFind1.FindFile(_T("*.*")); 
     
    while (bFindSuffix) 
    { 
        bFindSuffix = tempFind1.FindNextFile(); 
        CString strFilePath, strFileName; 
        if ( !tempFind1.IsDirectory() && !tempFind1.IsDots() ) 
        { 
            strFilePath = tempFind1.GetFilePath(); 
            strFileName = tempFind1.GetFileName(); 
            strFileName.MakeUpper();
            strSuffix.MakeUpper(); 
            if ( strFileName.Right(3) == strSuffix ) 
            { 
                arrPath.Add(strFilePath); 
            } 
        } 
    } 
    tempFind.Close(); 
    tempFind1.Close(); 
}

调用方法
    CStringArray arrFilePath;
    CString strTemp;
    FindMyFile(_T("d:\\temp\\"), _T("txt"), arrFilePath);
    for(int i=0; i<arrFilePath.GetSize(); i++) 
    { 
        strTemp += arrFilePath[i] + _T("\r\n");         
    } 
    AfxMessageBox(strTemp);

### Python 获取指定后缀文件 为了实现获取特定目录下具有指定后缀名的所有文件的功能,可以采用递归遍历的方式访问给定路径下的每一个子项,并检查它们是否为文件以及是否有匹配的目标扩展名。下面是一个具体的例子来展示这一过程: ```python import os def find_files_by_extension(directory, extensions): """ 查找并返回指定目录及其子目录中拥有特定后缀的所有文件。 参数: directory (str): 需要搜索的根目录。 extensions (tuple or str): 关心的一个或多个文件后缀(带点),如 ('.txt', '.log') 或者单个字符串形式 ".py". 返回: list: 符合条件的绝对路径组成的列表。 """ matched_files = [] for dirpath, _, filenames in os.walk(directory): for name in filenames: filepath = os.path.join(dirpath, name) if isinstance(extensions, tuple) and os.path.splitext(filepath)[1].lower() in extensions: matched_files.append(filepath) elif isinstance(extensions, str) and os.path.splitext(filepath)[1].lower() == extensions.lower(): matched_files.append(filepath) return matched_files # 使用示例 target_dir = r"C:\example_folder" exts_to_find = ('.jpg', '.png') result_paths = find_files_by_extension(target_dir, exts_to_find) print(result_paths) ``` 此函数`find_files_by_extension()`接受两个参数:一个是待扫描的顶级文件夹路径;另一个是要寻找的一种或多种类型的文件后缀。它会返回一个包含所有符合条件文件完整路径的列表[^3]。 对于更复杂的筛选逻辑或是其他高级操作,则可能需要用到第三方库或者其他标准库模块的支持,比如正则表达式来进行模式匹配等[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值