如何扫描文件夹中的所有文件,包括VC++2008中的子文件夹

How to scan all files in a folder, including subfolders in VC++ 2008?

本文关键字:文件夹 文件 包括 VC++2008 扫描 何扫描      更新时间:2023-10-16

我想在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)
             {
             }
        }
    }

此代码可能工作。。。。

#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("%sn", sf[i].c_str());
    }
    return 0;
}

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

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

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

使用findfirst和findnext()实现。有关详细信息,请查看下面的msdn链接。

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