2010-02-22 6 views
19

Ich möchte den Inhalt eines Verzeichnisses in einer Struktur mit C unter Windows auflisten und speichern.Verzeichnisinhalte mit C und Windows auflisten

Ich bin nicht unbedingt auf der Suche nach dem Code, den ich suche, eher zeigen Sie mir in die richtige Richtung, wenn es darum geht, welche Bibliothek ich betrachten sollte.

Ich google seit ein paar Stunden und alles, was ich finde, ist C#, C++ - Lösungen, so würde jede Hilfe sehr geschätzt werden.

+1

Ihre C++ Lösungen zeigen Ihnen, was API-Aufrufe Sie vornehmen müssen. –

Antwort

38

Genau wie jeder sagte, andere (mit Findfirstfile, Findnextfile und Findclose) ... aber mit Rekursion!

bool ListDirectoryContents(const char *sDir) 
{ 
    WIN32_FIND_DATA fdFile; 
    HANDLE hFind = NULL; 

    char sPath[2048]; 

    //Specify a file mask. *.* = We want everything! 
    sprintf(sPath, "%s\\*.*", sDir); 

    if((hFind = FindFirstFile(sPath, &fdFile)) == INVALID_HANDLE_VALUE) 
    { 
     printf("Path not found: [%s]\n", sDir); 
     return false; 
    } 

    do 
    { 
     //Find first file will always return "." 
     // and ".." as the first two directories. 
     if(strcmp(fdFile.cFileName, ".") != 0 
       && strcmp(fdFile.cFileName, "..") != 0) 
     { 
      //Build up our file path using the passed in 
      // [sDir] and the file/foldername we just found: 
      sprintf(sPath, "%s\\%s", sDir, fdFile.cFileName); 

      //Is the entity a File or Folder? 
      if(fdFile.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY) 
      { 
       printf("Directory: %s\n", sPath); 
       ListDirectoryContents(sPath); //Recursion, I love it! 
      } 
      else{ 
       printf("File: %s\n", sPath); 
      } 
     } 
    } 
    while(FindNextFile(hFind, &fdFile)); //Find the next file. 

    FindClose(hFind); //Always, Always, clean things up! 

    return true; 
} 

ListDirectoryContents("C:\\Windows\\"); 

Und jetzt seine UNICODE Pendant:

bool ListDirectoryContents(const wchar_t *sDir) 
{ 
    WIN32_FIND_DATA fdFile; 
    HANDLE hFind = NULL; 

    wchar_t sPath[2048]; 

    //Specify a file mask. *.* = We want everything! 
    wsprintf(sPath, L"%s\\*.*", sDir); 

    if((hFind = FindFirstFile(sPath, &fdFile)) == INVALID_HANDLE_VALUE) 
    { 
     wprintf(L"Path not found: [%s]\n", sDir); 
     return false; 
    } 

    do 
    { 
     //Find first file will always return "." 
     // and ".." as the first two directories. 
     if(wcscmp(fdFile.cFileName, L".") != 0 
       && wcscmp(fdFile.cFileName, L"..") != 0) 
     { 
      //Build up our file path using the passed in 
      // [sDir] and the file/foldername we just found: 
      wsprintf(sPath, L"%s\\%s", sDir, fdFile.cFileName); 

      //Is the entity a File or Folder? 
      if(fdFile.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY) 
      { 
       wprintf(L"Directory: %s\n", sPath); 
       ListDirectoryContents(sPath); //Recursion, I love it! 
      } 
      else{ 
       wprintf(L"File: %s\n", sPath); 
      } 
     } 
    } 
    while(FindNextFile(hFind, &fdFile)); //Find the next file. 

    FindClose(hFind); //Always, Always, clean things up! 

    return true; 
} 

ListDirectoryContents(L"C:\\Windows\\"); 
+1

Dies ist jedoch nicht "Unicode" -freundlich. – dreamlax

+3

Ja, es führt keine NTFS-Alternativ-Streams oder keine Backups. Aber er sagte nicht, dass er ein koreanisches Fotoalbum durchqueren wollte. Wie auch immer, es ist nur eine Probe. – NTDLS

+1

Ok ich gebe, quer durch das koreanische Fotoalbum ...: D – NTDLS

5

Um Dateiinhalte aufzulisten, können Sie ein Verzeichnis mit folgenden APIs durchsuchen: FindFirstFileEx, FindNextFile und CloseFind. Sie benötigen #include <windows.h, damit Sie Zugriff auf die Windows-API erhalten. Sie sind C-Funktionen und damit kompatibel mit C++. Wenn Sie "speziell C++" möchten, suchen Sie mithilfe von MFC nach Verzeichnissen.

+0

Benutzt er MFC? (Es ist der Teufel!) – NTDLS

+0

Ich weiß es nicht. Ich bin auch kein Fan, aber es gibt dir eine OO Sicht der Dinge. –

Verwandte Themen