簡體   English   中英

使用 Windows API 的文件夾樹的分層列表

[英]Hierarchical listing of a folder tree using Windows API

我使用此代碼在 C++ 中遞歸列出文件夾及其子文件夾,代碼工作正常,我從許多詢問遞歸文件夾列表的 Stackoverflow 問題中修改了它。

#include <iostream>
#include <string>
#include <windows.h>

#ifndef INVALID_FILE_ATTRIBUTES
#define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
#endif  

void findFiles (const std::string & spath)
{

struct HandleWrapper
{
   HANDLE hFind;        
  ~HandleWrapper () {if (hFind != INVALID_HANDLE_VALUE) ::FindClose (hFind); } 
} wrapper;

size_t i = 1;
WIN32_FIND_DATA FindFileData;

std::string sourcepath = spath + std::string ("\\*.*");
wrapper.hFind = FindFirstFile (sourcepath.c_str (), &FindFileData);


if (wrapper.hFind != INVALID_HANDLE_VALUE)

do
  {

  std::string fullpath =
  std::string (spath) + std::string ("\\") + std::string (FindFileData.cFileName);

  if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && std::string FindFileData.cFileName) != "." && std::string (FindFileData.cFileName) != "..")
  
    findFiles (fullpath);

  else
   
    std::cout << i++ << "-" << FindFileData.cFileName<<std::endl;
  
  } while (FindNextFile (wrapper.hFind, &FindFileData));

}

int main (int argc, char **argv)
{
std::string spath (argv[1]);
findFiles (spath);
return EXIT_SUCCESS;
}

問題是如果你有一棵樹是這樣的:

A file
B another file
C folder
D another folder
E file
F file

該程序將列出A and B然后進入C及其可能包含的任何子文件夾,然后對D執行相同的操作,然后將列出父文件夾中的文件“E 和 F”,我真的希望它列出A, B, E and F然后轉到子文件夾C and D 任何想法在不求助於外部圖書館的情況下做到這一點?

正如@Shubham 所說,您需要使用隊列來完成您想要的功能。

遍歷文件夾時,如果遇到文件夾,則將其推入隊列,遍歷完成后將其從隊列中彈出,從而實現對文件夾的廣度遍歷。

這是示例:

#include <Windows.h>
#include <iostream>
#include <queue>
using namespace std;
queue<std::string> qFolders;

void findFiles(string Path)
{
    WIN32_FIND_DATA findResult;
    HANDLE handle = NULL;

    if (qFolders.size() > 0)
    {
        std::string tempFolder = qFolders.front();
        tempFolder.append("\\*.*");
        handle = FindFirstFile(tempFolder.c_str(), &findResult);
        cout << qFolders.front() << endl;
        do
        {
            if (findResult.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                if (lstrcmp(".", findResult.cFileName) == 0 || lstrcmp("..", findResult.cFileName) == 0)
                {
                    continue;
                }
                tempFolder = qFolders.front();
                tempFolder.append("\\").append(findResult.cFileName);
                qFolders.push(tempFolder);
            }
            else {
                cout << "  " << findResult.cFileName << endl;
            }
        } while (FindNextFile(handle, &findResult));
        qFolders.pop();
        if(!qFolders.empty())
        {
            findFiles(qFolders.front());
        }
    }
    if (handle)
    {
        FindClose(handle);
        handle = NULL;
    }
}

int main()
{
    qFolders.push("D:\\test\\t");
    findFiles(qFolders.front());
    return 0;
}

輸出:

在此處輸入圖片說明

使用隊列。 如果遇到文件夾,請將其推送到隊列中。 當當前目錄中的所有子節點都完成后,然后從隊列中拉出並潛入並重復,更像是一個修改后的 BFS。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM