views:

515

answers:

4

I'm looking to list and store the contents of a directory in a struct using C on Windows.

I'm not necessarily looking for anyone to write out the code I'm looking for, rather point me in the right direction when it comes to which library I should be looking at.

I've been Googling for a few hours now and all I'm finding is C#, C++ solutions so any help would be greatly appreciated.

+1  A: 

C and C++ are pretty compatible in many ways... As far as I know all of the Win32 API File Management C++ functions can be used from C

John Weldon
Win32 *is* a C API, isn't it?
dreamlax
Yes, I thought so too, but for some reason the MSDN docs show examples and call them *C++* examples. I'm fairly confident that the Win32 API wasn't intended for only C++.
John Weldon
+4  A: 

To list file contents you can search a directory with these APIs: FindFirstFileEx, FindNextFile and CloseFind. You'll need to #include <windows.h, that'll get you access to the Windows API. They're C functions and so compatible with C++. If you want "specifically C++", try searching for listing directories using MFC.

Ninefingers
Is he using MFC? (It's the devil!)
NTDLS
I don't know. I'm no fan either, but it does give you an OO view of things.
Ninefingers
+3  A: 

Hi,

Probably You are looking for these functions.

FindFirstFile:

FindNextFile:

FindClose:

lollinus
+1  A: 

Just like everyone else said (with FindFirstFile, FindNextFile and FindClose)... but with recursion!

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

And now its UNICODE counterpart:

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\\");
NTDLS
This is not "Unicode"-friendly though.
dreamlax
Yea, It doesnt list NTFS alternate streams or do any backups either. But he didnt say he wanted to traverse a Korean picture album. Either way, it's just a sample.
NTDLS
Ok I give, traverse that Korean picture album... :D
NTDLS