Skip to content

Instantly share code, notes, and snippets.

@albertofwb
Last active November 14, 2019 09:12
Show Gist options
  • Save albertofwb/bf984b3a5b9af5488e1719cf84cd2378 to your computer and use it in GitHub Desktop.
Save albertofwb/bf984b3a5b9af5488e1719cf84cd2378 to your computer and use it in GitHub Desktop.
广度优先枚举windows 文件路径
#include <Windows.h>
#include <deque>
#include <string>
void ListFilesBFS(const std::string& root_dir)
{
std::deque<std::string> output;
WIN32_FIND_DATA findfiledata;
HANDLE hFind = INVALID_HANDLE_VALUE;
output.push_back(root_dir);
while (!output.empty()) {
std::string current_dir = output.front();
output.pop_front();
printf("list %s\n", current_dir.c_str());
hFind = FindFirstFile((LPCSTR)(current_dir + "\\*").c_str(), &findfiledata);
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
if (strcmp(findfiledata.cFileName, ".") == 0 || strcmp(findfiledata.cFileName, "..") == 0)
{
continue;
}
std::string fullpath = current_dir + "\\" + findfiledata.cFileName;
if ((findfiledata.dwFileAttributes | FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
{
//printf("%s\n", fullpath.c_str());
output.push_back(fullpath);
}
else {
// do something
}
} while (FindNextFile(hFind, &findfiledata) != 0);
}
}
}
int main() {
ListFilesBFS("D:");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment