2010-02-22 16 views
19

Estoy buscando listar y almacenar los contenidos de un directorio en una estructura usando C en Windows.Listado de contenido del directorio usando C y Windows

No necesariamente estoy buscando a alguien que escriba el código que estoy buscando, más bien apúnteme en la dirección correcta cuando se trata de qué biblioteca debería estar mirando.

He estado buscando en Google por unas horas y todo lo que estoy buscando son soluciones C#, C++ por lo que cualquier ayuda sería muy apreciada.

+1

Sus soluciones C++ le mostrará lo API llamadas que necesita hacer. –

Respuesta

38

Al igual que todos los demás dijo (con FindFirstFile, FindNextFile y FindClose) ... pero con la repetición!

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

Y ahora su contraparte 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

Sin embargo, esto no es "Unicode". – dreamlax

+3

Sí, no enumera NTFS flujos alternativos o hacer copias de seguridad tampoco. Pero él no dijo que quería atravesar un álbum de fotos coreano. De cualquier manera, es solo una muestra. – NTDLS

+1

Ok doy, atravieso ese álbum de fotos coreano ...: D – NTDLS

5

Para enumerar los contenidos de los archivos, puede buscar en un directorio con estas API: FindFirstFileEx, FindNextFile y CloseFind. Necesitarás #include <windows.h, que te dará acceso a la API de Windows. Son funciones C y son tan compatibles con C++. Si desea "específicamente C++", intente buscar directorios de listados utilizando MFC.

+0

¿Está usando MFC? (¡Es el diablo!) – NTDLS

+0

No lo sé. Tampoco soy un fanático, pero te da una visión de OO de las cosas. –

Cuestiones relacionadas