How to recursively traverse directories in C on Windows

18,182

Solution 1

Here is how you do it (this is all from memory so there may be errors):

void FindFilesRecursively(LPCTSTR lpFolder, LPCTSTR lpFilePattern)
{
    TCHAR szFullPattern[MAX_PATH];
    WIN32_FIND_DATA FindFileData;
    HANDLE hFindFile;
    // first we are going to process any subdirectories
    PathCombine(szFullPattern, lpFolder, _T("*"));
    hFindFile = FindFirstFile(szFullPattern, &FindFileData);
    if(hFindFile != INVALID_HANDLE_VALUE)
    {
        do
        {
            if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                // found a subdirectory; recurse into it
                PathCombine(szFullPattern, lpFolder, FindFileData.cFileName);
                FindFilesRecursively(szFullPattern, lpFilePattern);
            }
        } while(FindNextFile(hFindFile, &FindFileData));
        FindClose(hFindFile);
    }

    // Now we are going to look for the matching files
    PathCombine(szFullPattern, lpFolder, lpFilePattern);
    hFindFile = FindFirstFile(szFullPattern, &FindFileData);
    if(hFindFile != INVALID_HANDLE_VALUE)
    {
        do
        {
            if(!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
            {
                // found a file; do something with it
                PathCombine(szFullPattern, lpFolder, FindFileData.cFileName);
                _tprintf_s(_T("%s\n"), szFullPattern);
            }
        } while(FindNextFile(hFindFile, &FindFileData));
        FindClose(hFindFile);
    }
}

So you could call this like

FindFilesRecursively(_T("C:\\WINDOWS"), _T("*.wav"));

to find all the *.wav files in C:\WINDOWS and its subdirectories.

Technically you don't have to do two FindFirstFile() calls, but I find the pattern matching functions Microsoft provides (i.e. PathMatchFileSpec or whatever) aren't as capable as FindFirstFile(). Though for "*.wav" it would probably be fine.

Solution 2

Based on your mention of .wav, I'm going to guess you're writing code for Windows (that seems to be where *.wav files are most common). In this case, you use FindFirstFile and FindNextFile to traverse directories. These use a WIN32_FIND_DATA structure, which has a member dwFileAttributes that contains flags telling the attributes of the file. If dwAttributes & FILE_ATTRIBUTE_DIRECTORY is non-zero, you have the name of a directory.

Solution 3

Very Helpful. I had anyway, a stack overflow since it was always adding "." to the path and returning to the same path = endless loop.

Adding this solved it:

// found a subdirectory; recurse into it PathCombine(szFullPattern, lpFolder, FindFileData.cFileName); FindFilesRecursively(szFullPattern, lpPattern); if (FindFileData.cFileName[0] == '.') continue;

Share:
18,182
pmilb
Author by

pmilb

Manchester United is life!

Updated on July 19, 2022

Comments

  • pmilb
    pmilb almost 2 years

    Ultimately I want to travel through a folder's files and subdirectories and write something to all files i find that have a certain extension(.wav in my case). when looping how do i tell if the item I am at is a directory?

  • pmilb
    pmilb over 14 years
    oops, forgot to say I'm doing this on windoze also.
  • pmilb
    pmilb over 14 years
    Yeah, I'm doing this on Windows. My C is really rusty, haven't programmed in C for a few years. I was trying to use dirent.h, but I dont think it offers any way of telling if the item is a directory or not. Can't find a good example of FindFirstFile from google
  • Carsten
    Carsten over 14 years
    This page shows how to use them (in VB, but translation to C should be more or less straight forward): support.microsoft.com/kb/185476. Note that you might also want to think about how you hande NTFS junctions, i.e. whether to follow them or not, and if you do how to avoid endless loops.
  • Synetech
    Synetech almost 9 years
    Besides a couple of minor typeos it works great. @NeilWeicher there’s an edit button so that you can fix those minor typos.
  • Synetech
    Synetech almost 9 years
    That would exclude all directories starting with a period (including hidden directories that come from *nix systems).