C++选择文件夹并获得其中所有文件路径

运行环境:

编译器:mingw-g++ v12.2.0   文本编辑器:vscode

总体思路为调出文件夹选择窗口,获取用户选择的文件夹地址,在这个地址中递归查找文件名和文件绝对路径,会在控制台打印所有找到的文件名并在当前目录生成一个“result.txt”文件储存结果,main()函数中没有加入system('pause');语句,有需求可以自己加上去。

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <io.h>
#include <ShlObj.h>
#include <tchar.h>

void getFileNames(std::string path, std::vector<std::string> &files);
std::string selectdir();

int main()
{
    std::vector<std::string> fileNames;
    std::string path;
    path = selectdir();

    getFileNames(path, fileNames);
    std::ofstream outfile;
    outfile.open("result.txt", std::ios::trunc | std::ios::out);
    for (const auto &ph : fileNames)
    {
        if (&ph == &fileNames.back())
        {
            std::cout << ph;
            outfile << ph;
            break;
        }
        std::cout << ph << "\n";
        outfile << ph << "\n";
    }
    outfile.close();

    return 0;
}

void getFileNames(std::string path, std::vector<std::string> &files)
{
    intptr_t hFile = 0;
    struct _finddata_t fileinfo;
    std::string p;

    if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
    {
        do
        {
            if ((fileinfo.attrib & _A_SUBDIR))
            {
                if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
                {
                    getFileNames(p.assign(path).append("\\").append(fileinfo.name), files);
                }
            }
            else
            {
                files.push_back(p.assign(path).append("\\").append(fileinfo.name));
            }
        } while (_findnext(hFile, &fileinfo) == 0);
        _findclose(hFile);
    }
}

std::string selectdir()
{
    TCHAR dirpath[MAX_PATH] = {0};
    BROWSEINFO sd;
    ZeroMemory(&sd, sizeof(BROWSEINFO));
    sd.hwndOwner = NULL;
    sd.pszDisplayName = dirpath;
    sd.lpszTitle = _T("Select a directory:");
    sd.ulFlags = BIF_RETURNFSANCESTORS;
    LPITEMIDLIST idl = SHBrowseForFolder(&sd);
    if (NULL == idl)
    {
        exit(0);
    }
    SHGetPathFromIDList(idl, dirpath);

    std::string result = (char *)dirpath;

    return result;
}

总体思路还是很简单的,但想要彻底弄懂还是要查阅很多资料,说一下其中的注意事项:

getFileNames()函数中的struct _finddata_在"io.h"头文件中,其结构为

struct _finddata_t
{
    unsigned attrib;          //文件属性
    time_t time_create;       //1970年1月1日0时0分0秒到现在时刻的秒数(time_t即long)
    time_t time_access;       //文件上一次访问时间
    time_t time_write;        //文件上一次修改时间
    _fsize_t size;            //文件大小(字节 _fsize_t即long)
    char name[_MAX_FNAME];    //文件名(_MAX_FNAME为文件名的最大长度 一般为260)
};

相关操作函数有三个:

1)long _findfirst( char *filespec, struct _finddata_t *fileinfo );

第一个参数为文件名字符串,支持通配符表示,第二个参数为_finddata_t的结构体,此结构体只需要事先声明,不需要初始化,返回值为long型,作为文件句柄供_findnext()函数使用。

2)int _findnext( long handle, struct _finddata_t *fileinfo );

第一个参数为_findfirst()函数返回的文件句柄,第二个参数为为_finddata_t的结构体,返回值为0或-1分别表示储存文件信息成功或失败。

3)int _findclose( long handle );

参数为_findfirst()函数返回的文件句柄,此函数会关闭此次查找。

_A_SUBDIR是文件属性,表示文件夹类型,符号&为按位与操作符。do{}代码块里的if{}代码块表示查找到的文件是文件夹,需要递归查找,else{}代码块表示查找到的是文件,储存其绝对路径。

selectdir()函数就是常规调包使用了,其中的TCHAR等字符在"tchar.h"头文件中,char长度为8位,不能表示中文,查找文件名要使用支持中文的TCHAR,TCHAR可以作为wchar_t或者char使用,最后得到的txt文件会被编码为ANSI,在vscode中要手动使用GB2312编码查看(也许把TCHAR转化为wchar_t再转std::string可以避免这个麻烦?)。

参考文档:

C++ 快速获取文件夹(目录)下的所有文件名

C++文件(夹)选择对话框

_finddata_t结构体用法

C++字符类型TCHAR、char和wchar_t

感谢观看

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值