2010-02-22 5 views
19

Je cherche à lister et stocker le contenu d'un répertoire dans une structure en utilisant C sous Windows. Je ne cherche pas forcément quelqu'un pour écrire le code que je cherche, mais plutôt moi dans la bonne direction quand il s'agit de la bibliothèque que je devrais regarder.Affichage du contenu du répertoire en utilisant C et Windows

J'ai été google pendant quelques heures maintenant et tout ce que je trouve est des solutions C#, C++ donc toute aide serait grandement appréciée.

+1

C++ solutions vous montrer ce que les appels d'API que vous devez faire. –

Répondre

38

Comme tout le monde dit autre (avec FindFirstFile, FindNextFile et FindClose) ... mais avec récursion!

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\\"); 

Et maintenant son homologue UNICODE:

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

Ce n'est cependant pas "Unicode". – dreamlax

+3

Oui, il ne répertorie pas les flux de remplacement NTFS ou ne fait aucune sauvegarde non plus. Mais il n'a pas dit qu'il voulait traverser un album photo coréen. De toute façon, c'est juste un échantillon. – NTDLS

+1

Ok je donne, traverse cet album photo coréen ...: D – NTDLS

5

Pour répertorier le contenu d'un fichier, vous pouvez rechercher un répertoire avec ces API: FindFirstFileEx, FindNextFile et CloseFind. Vous aurez besoin de #include <windows.h, cela vous donnera accès à l'API Windows. Ce sont des fonctions C et donc compatibles avec C++. Si vous voulez "spécifiquement C++", essayez de rechercher des répertoires de listes à l'aide de MFC.

+0

Utilise-t-il le MFC? (C'est le diable!) – NTDLS

+0

Je ne sais pas. Je ne suis pas non plus un fan, mais cela vous donne une vision OO des choses. –

Questions connexes