【问题标题】:How to scan all files in a folder, including subfolders in VC++ 2008?如何扫描文件夹中的所有文件,包括 VC++ 2008 中的子文件夹?
【发布时间】:2012-07-15 23:08:23
【问题描述】:

我想在 VC++ 2008(Windows 窗体应用程序)中构建一个应用程序。

在这里我想浏览我选择的文件夹(按钮“浏览”),这样当我按下“扫描”按钮时,我的应用程序将找到我选择的文件夹中的所有文件,包括子文件夹。然后所有文件都放在一个列表框中,我有这段代码会在c#中而不是在c++中,如何在c++中更改我的代码?

private void btnScan_Click_1(object sender, EventArgs e)
    {

        List<string> search = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.*", SearchOption.AllDirectories).ToList();
        progressBar1.Maximum = search.Count;
        //foreach (Directory.GetDirectories.search))

        foreach(string item in search)
        {
            try
             {
                StreamReader stream = new StreamReader(item);
                string read = stream.ReadToEnd();
                foreach(string st in viruslist)
                {
                if(Regex.IsMatch(read,st))
                {
                viruses+=1;
                    label1.Text+= viruses;
                    listBox1.Items.Add(item);
                }
                progressBar1.Increment(1);
                }

             }
             catch(Exception ex)
             {

             }
        }
    }

【问题讨论】:

  • 这取决于您是尝试创建托管 C++ CLR 应用程序还是本地 Windows C++ Win32 应用程序。如果它是托管的,您可以使用.Net。如果不是,您将需要使用 Win32 或 Win32 包装器。

标签: c++ file visual-c++ directory visual-c++-2008


【解决方案1】:

此代码可能有效....

#include <Windows.h>

#include <stdio.h>
#include <stdlib.h>

#include <vector>
#include <string>

struct SearchFile
{
    typedef std::vector<std::string> FileNameArray;
    FileNameArray files;

    FileNameArray::iterator begin()
    {
        return files.begin();
    }

    FileNameArray::iterator end()
    {
        return files.end();
    }

    int count() const
    {
        return (int)files.size();
    }

    std::string operator[](int index)
    {
        return files[index];
    }

    void operator()(const std::string &path, const std::string &pattern)
    {
        WIN32_FIND_DATA wfd;
        HANDLE hf;
        std::string findwhat;
        std::vector<std::string> dir;

        findwhat = path + "\\*";  // directory

        hf = FindFirstFile(findwhat.c_str(), &wfd);
        while (hf != INVALID_HANDLE_VALUE)
        {
            if (wfd.cFileName[0] != '.' && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
            {
                std::string found;

                found = path + "\\" + wfd.cFileName;
                dir.push_back(found);
            }

            if (!FindNextFile(hf, &wfd))
            {
                FindClose(hf);
                hf = INVALID_HANDLE_VALUE;
            }
        }

        findwhat = path + "\\" + pattern;  // files

        hf = FindFirstFile(findwhat.c_str(), &wfd);
        while (hf != INVALID_HANDLE_VALUE)
        {
            if (wfd.cFileName[0] != '.' && !(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
            {
                std::string found;

                found = path + "\\" + wfd.cFileName;
                files.push_back(found);
            }

            if (!FindNextFile(hf, &wfd))
            {
                FindClose(hf);
                hf = INVALID_HANDLE_VALUE;
            }
        }

        // continue with directories
        for (std::vector<std::string>::iterator it = dir.begin(); it != dir.end(); ++it)
            this->operator()(*it, pattern);
    }
};

int main(void)
{
    SearchFile sf;

    // get all .jpg files in current dir
    sf(".", "*.jpg");

    for (int i = 0; i != sf.count(); ++i)
    {
        printf("%s\n", sf[i].c_str());
    }

    return 0;
}

【讨论】:

  • 是的,它工作,但我想使用 Windows 应用程序而不是控制台应用程序...你能帮我吗?谢谢
  • @mapix 只需将 SearchFile 结构复制到您的 win32 应用程序项目中,然后像在控制台应用程序中一样使用它。如果您使用 UNICODE,也许您必须更改一些字符串操作。
【解决方案2】:

使用 findfirst 和 findnext() 来实现。查看下面的 msdn 链接了解更多详细信息。

http://msdn.microsoft.com/en-us/library/zyzxfzac(v=vs.71).aspx

【讨论】:

    【解决方案3】:

    由于您有一个 .NET 应用程序(因为您使用的是 Windows 窗体),最简单的方法是使用 System::IO::Directory::GetFiles() 列出文件夹和所有子文件夹中的所有文件。

    array<String^>^ files = GetFiles(folder, "*.*", System::IO::SearchOption::AllDirectories);
    

    AllDirectories 选项使 GetFiles 搜索所有子文件夹中的文件。

    【讨论】:

    • 是的,我的意思是喜欢这个代码,但是你能告诉我完整的代码吗?
    猜你喜欢
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 2010-10-20
    • 2017-07-05
    相关资源
    最近更新 更多