如何扫描文件夹中的所有文件,包括VC++2008中的子文件夹
How to scan all files in a folder, including subfolders in VC++ 2008?
我想在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
相关文章:
- 如何将更多文件夹添加到c++include路径
- GetShortPathName在网络驱动器上使用中文文件夹时失败
- VS2017,C++包含目录与附加包含目录,子文件夹包含失败-但为什么
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 如何在文档文件夹中创建目录
- "assimp/config.h"找不到,但它在文件夹中
- C++入门 5 版:类消息和文件夹
- 我可以在 Arduino 的库文件夹之外安装库吗?
- 如何按文件扩展名引用文件夹中的文件
- 在QTreeView中仅显示共享驱动器和文件夹
- 加载与引用 .NET DLL 位于同一文件夹中的引用的 .NET DLL 时"Not found"异常
- 介子复制/安装头文件到输出目录并保持文件夹结构
- vcruntime.h 从 Windows 包含文件夹中丢失?
- 在文件夹迭代上实现 RAII
- 使用 Powershell 命令将 cpp 文件的文件夹编译为 GNU 的 g++
- 使文件夹中的所有文件可供所有项目(子目录)访问
- 无法使用 CMake 从其他文件夹添加源文件
- 如何在安卓工作室中将c ++头文件从一个文件夹导入到另一个文件夹?
- 对单元测试(googletest)和项目/文件夹/文件的混淆
- 设计文件夹/文件系统