C ++查找所有.wav文件

时间:2013-06-23 23:45:12

标签: c++ file search directory

我正在尝试用C ++创建一个应用程序来搜索磁盘上的所有.wav文件(包括所有子文件夹及其子文件夹等等......,整个磁盘搜索,如Avast上的防病毒搜索等)和将它们存储在一个字符串中,稍后保存到文本文件中。我正在弄清楚如何实际做到这一点的方法,我可以使用一些帮助。

这是我到目前为止所做的,它只搜索C盘根文件夹,我现在不知道该怎么做。我知道我应该使用某种类型的循环,但我不知道如何实际操作,没有合理的方法我可以这样做。

void finddata()
{
    MessageBox(NULL, "finddata called", "Started", MB_OK | MB_ICONINFORMATION);
    WIN32_FIND_DATA FindFileData; // A pointer to the WIN32_FIND_DATA structure that receives information about a found file or directory.
    HANDLE hFind; // Used to check the return value
    hFind = FindFirstFile("C://*.wav", &FindFileData);
    if(hFind == INVALID_HANDLE_VALUE)
    {
        MessageBox(NULL, "INVALID_HANDLE_VALUE", "Failed", MB_OK | MB_ICONINFORMATION);
    }
    else
    {
        string file;
        file = FindFileData.cFileName;
        file.append("|");
        MessageBox(NULL, file.c_str(), "YO", MB_OK | MB_ICONASTERISK); // string.c_str() to conv to LPCSTR
        FindClose(hFind);
    }
}

最后的MessageBox就在那里作为测试,我稍后会删除它。我目前正在学习C ++,这是一个初学者的项目,所以我可以掌握它。

此外,字符串数据类型的大小是否有任何限制?感谢。

2 个答案:

答案 0 :(得分:2)

显然,你确实需要FindNextFile调用,并使用某种循环重复该调用,直到它返回“没有更多文件”的返回代码。

要搜索整个磁盘,您需要在“当前目录”(根目录)中查找目录,并且每次搜索目录时都可以通过递归调用finddata来执行此操作。 (将目录的名称作为参数添加到函数中),或在代码中实现堆栈以跟踪您所在的目录,以及哪一个“返回”以进入下一级别。

我故意不是为你​​编写代码,而是描述你需要做什么,因为你是一个学习编程。我在1985年左右做过这种事。

答案 1 :(得分:1)

递归是方法。

这是一个可能的实现(为了灵感,但是以你自己的方式去做):

// Filtering by the given extension
void AppendFile(list<string>& fileList, const string& directory, const string& fileName, const string& extFilter)
{
    string fileExtension = fileName.substr(fileName.find_last_of(".") + 1);
    if (!fileExtension.empty())
    {
        // Extension doesn't match: return
        if (extFilter.find(fileExtension) == string::npos)
            return;
    }
    // If we have a match: append file to list
    fileList.push_back(directory + fileName);
}

// Getting the files of the given extension recursively (or not)
void GetFiles(list<string>& fileList, string directory, const string& extFilter, bool recursively = true)
{
    string filePath;

    directory += "\\";
    string filter = directory + "*";

    WIN32_FIND_DATA FindFileData;
    HANDLE hFind = FindFirstFile(filter.c_str(), &FindFileData);

    if (hFind == INVALID_HANDLE_VALUE) return;

    do
    {
        // If we find a (sub-)directory
        if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
            // Here comes the recursive call
            if ((recursively) && (lstrcmp(FindFileData.cFileName, TEXT(".")) != 0) && (lstrcmp(FindFileData.cFileName, TEXT("..")) != 0))
                // The 'GetFiles()' calls itself on the sub-directory
                GetFiles(fileList, directory + FindFileData.cFileName, extFilter);
        }
        else
            // If we find a file: simply append it to the list
            AppendFile(fileList, directory, FindFileData.cFileName, extFilter);
    }
    while (FindNextFile(hFind, &FindFileData) != 0);

    FindClose(hFind);
}

用法:

list<string> fileList;
GetFiles(fileList, "C:\\root_dir", "wav");

for(list<string>::iterator it = fileList.begin(); it != fileList.end(); ++it)
{
    cout << (*it) << endl;
}