C ++如何在一个文件夹中查找大量文件?

时间:2019-09-02 20:58:23

标签: c++ windows

我正在制作类似于ls或dir的命令行程序,我想显示一条消息,告诉您该文件夹中有多少个文件和目录。我可以在C ++中使用已有的功能做什么?

我正在使用ostringstream创建一个列表,然后使用fd。访问文件详细信息。

这是专门针对Windows的。

struct List
{
    vector<string> files;
    vector<string> folders;
    vector<FILETIME> time;
    vector<int> size;

    //add other things to this list, like permissions??
};

struct List get_all_files_names_within_folder(string folder)
{

    vector<string> files;
    vector<string> folders;
    vector<FILETIME> time;
    vector<int> size;

    char search_path [500];
    sprintf_s(search_path, "%s*.*", folder.c_str());
    WIN32_FIND_DATA fd;
    HANDLE hFind = ::FindFirstFile(search_path, &fd);

    if (hFind != INVALID_HANDLE_VALUE)
    {
        do
        {
            if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
            {
                files.push_back(fd.cFileName);
                time.push_back(fd.ftLastWriteTime);
                size.push_back(fd.nFileSizeLow);
            }
            else
            {
                folders.push_back(fd.cFileName);
                time.push_back(fd.ftLastWriteTime);
            }
        } while (::FindNextFile(hFind, &fd));
        ::FindClose(hFind);
    }

    List me;
    me.files = files;
    me.folders = folders;
    me.time = time;
    me.size = size;

    return me;
}

0 个答案:

没有答案