How to scan all files in a folder, including subfolders in VC++ 2008?
-
21-06-2021 - |
题
I'm want to build an application in VC++ 2008 (Windows form application).
Here I want to browse a folder of my choice (push button "browse"), such that when I press the "scan" button my application will find all the files in the folder I chose, including subfolders. Then all files are placed in a listbox, I have this code will be in c# not in c++, how to change my code in 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)
{
}
}
}
解决方案
Since your have a .NET application ( since you are using Windows Forms) the easiest way is to use System::IO::Directory::GetFiles() list all of the files in a folder and all subfolders.
array<String^>^ files = GetFiles(folder, "*.*", System::IO::SearchOption::AllDirectories);
The AllDirectories option causes GetFiles to search for files in all sub folders.
其他提示
This code might work....
#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;
}
Use findfirst and findnext () to implement. check below msdn link for more details.
http://msdn.microsoft.com/en-us/library/zyzxfzac(v=vs.71).aspx