2010-02-22 41 views
19

Tôi đang tìm cách liệt kê và lưu trữ nội dung của một thư mục trong cấu trúc bằng C trên Windows.Liệt kê nội dung thư mục bằng C và Windows

Tôi không nhất thiết phải tìm bất kỳ ai để viết mã mà tôi đang tìm kiếm, thay vì chỉ cho tôi đúng hướng khi nói đến thư viện nào tôi nên xem.

Tôi đã được Googling trong vài giờ và tất cả những gì tôi đang tìm kiếm là giải pháp C#, C++ nên mọi trợ giúp sẽ được đánh giá cao.

+1

C giải pháp của bạn ++ sẽ cho bạn thấy những gì API gọi cho bạn cần phải thực hiện. –

Trả lời

38

Cũng giống như mọi người khác nói (với FindFirstFile, FindNextFile và FindClose) ... nhưng với đệ quy!

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

Và bây giờ đối UNICODE của nó:

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

Mặc dù đây không phải là "Unicode". – dreamlax

+3

Yea, Nó không liệt kê các luồng thay thế NTFS hoặc thực hiện bất kỳ bản sao lưu nào. Nhưng anh không nói anh muốn đi qua một album ảnh Hàn Quốc. Dù bằng cách nào, nó chỉ là một mẫu. – NTDLS

+1

Ok tôi cho, duyệt qua album ảnh Hàn Quốc ...: D – NTDLS

5

Để liệt kê nội dung tệp, bạn có thể tìm kiếm thư mục có các API sau: FindFirstFileEx, FindNextFileCloseFind. Bạn sẽ cần phải #include <windows.h, điều đó sẽ giúp bạn truy cập vào API Windows. Chúng là các hàm C và tương thích với C++. Nếu bạn muốn "cụ thể C++", hãy thử tìm kiếm thư mục danh sách bằng MFC.

+0

Anh ấy đang sử dụng MFC? (Đó là ma quỷ!) – NTDLS

+0

Tôi không biết. Tôi cũng không có fan, nhưng nó cho bạn một cái nhìn OO về mọi thứ. –

Các vấn đề liên quan