Список содержания каталога с помощью C и Windows

Я надеюсь перечислять и хранить содержание каталога в структуре с помощью C в Windows.

Я не обязательно ищу любого для выписывания кода, который я ищу, скорее указываю на меня в правильном направлении, когда дело доходит до которой библиотеки я должен смотреть на.

Я Гуглил в течение нескольких часов теперь и всего, что я нахожу, C#, решения для C++, таким образом, любая справка значительно ценилась бы.

20
задан Flyer1 22 February 2010 в 21:56
поделиться

3 ответа

Как и все остальные (с FindFirstFile, FindNextFile и FindClose) ... но с рекурсией!

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

А теперь его аналог 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\\");
40
ответ дан 29 November 2019 в 23:11
поделиться

Возможно, вы ищете эти функции: FindFirstFile, FindNextFile, and FindClose.

7
ответ дан 29 November 2019 в 23:11
поделиться

Чтобы перечислить содержимое файлов, можно выполнить поиск в каталоге с помощью этих API: FindFirstFileEx, FindNextFile and CloseFind. Вам потребуется #include , это даст вам доступ к Windows API. Это функции языка C, поэтому они совместимы с C++. Если вам нужен "конкретно C++", попробуйте поискать каталоги листинга с помощью MFC.

5
ответ дан 29 November 2019 в 23:11
поделиться
Другие вопросы по тегам:

Похожие вопросы: