使用win32阅读子目录

Reading Subdirectories with win32

本文关键字:子目录 win32 使用      更新时间:2023-10-16

我正在尝试用win32函数读取一些子目录,结果是这样的。大多数事情都很顺利。我还没有完全运行这个函数,因为我还在调试它。我的问题是:我有5个实际的文件和两个子目录。当我试图获取目录中每个子目录和文件的文件名时,我会得到以下信息:"."、".."、"Subdirectory1"、"subdirectory"、"其余文件"。。。为什么我会得到一个句点,两个句点,然后是文件夹中的实际文件?

static std::vector<std::string> ReadAllFilesIntoArray(std::string contentDirPath, std::string fileType)
{
    std::vector<std::string> filePaths;
    std::wstring strTemp;
    strTemp.assign(contentDirPath.begin(), contentDirPath.end());
    HANDLE hFile = INVALID_HANDLE_VALUE;
    WIN32_FIND_DATA FindFileData;
    hFile = FindFirstFile(strTemp.c_str(), &FindFileData);
    if (INVALID_HANDLE_VALUE != hFile)
    {
        int i = 0;
        do{
            // If it's a directory
            if (FILE_ATTRIBUTE_DIRECTORY & FindFileData.dwFileAttributes)
            {
                // Convert wchar[260] -> std::string
                char ch[260];
                char DefChar = ' ';
                WideCharToMultiByte(CP_ACP, 0, FindFileData.cFileName, -1, ch, 260, &DefChar, NULL);
                std::string ss(ch);
                std::vector<std::string> localFilePaths = ReadAllFilesIntoArray(contentDirPath.assign(contentDirPath.begin(), contentDirPath.end() - 5) + ss + "//*", fileType);
                // Append the file paths found in the subdirectory to the ones found in the current directory
                filePaths.insert(filePaths.begin(), localFilePaths.begin(), localFilePaths.end());
            }
            // Convert wchar[260] -> std::string
            char ch[260];
            char DefChar = ' ';
            WideCharToMultiByte(CP_ACP, 0, FindFileData.cFileName, -1, ch, 260, &DefChar, NULL);
            std::string tempString(ch);
            // Then add to list if it's equal to the file type we are checking for
            if (tempString.substr(tempString.size() - 3, tempString.size()) == fileType)
            {
                filePaths.resize(i + 1);
                filePaths[i] = ch;
                i++;
            }
        } while (FindNextFile(hFile, &FindFileData));
        FindClose(hFile);
    }
    return filePaths;
}

它们是表示当前目录(.)和父目录(..)的特殊名称。枚举代码通常使用特殊情况检查来编写,以忽略这两个特殊值。