Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

recursive file search

I'm trying to figure out how to work this thing out .. For some reason, it ends at a certain point.. I'm not very good at recursion and I'm sure the problem lies somewhere there..

Also, even if I checked for cFileName != "..", it still shows up at the end, not sure why but the "." doesn't show up anymore..

void find_files( wstring wrkdir )
{
    wstring temp;

    temp = wrkdir + L"\\" + L"*"; 
    fHandle = FindFirstFile( temp.c_str(), &file_data );

    if( fHandle == INVALID_HANDLE_VALUE )
    {
         return;
    }
    else 
    { 
        while( FindNextFile( fHandle, &file_data ) ) 
        {
            if( file_data.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY &&
                wcscmp(file_data.cFileName, L".") != 0 && 
                        wcscmp(file_data.cFileName, L"..") != 0 )
            {
                find_files( wrkdir + L"\\" + file_data.cFileName  );
            }
            else if( file_data.dwFileAttributes != FILE_ATTRIBUTE_HIDDEN && 
                 file_data.dwFileAttributes != FILE_ATTRIBUTE_SYSTEM  )
            {
                results << wrkdir << "\\" << file_data.cFileName << endl;
            }
        }
    }
}

After changing those, the program doesn't enumerate the remaining files left..

For example, if there is a sub folder named test, it enumerates everything inside test but doesn't finish enumerating the files inside the original directory specified.

like image 408
Charles Khunt Avatar asked Sep 09 '26 22:09

Charles Khunt


2 Answers

From the FindFirstFile documentation:

If the function fails or fails to locate files from the search string in the lpFileName parameter, the return value is INVALID_HANDLE_VALUE and the contents of lpFindFileData are indeterminate.

You should only exit from the one iteration not the whole program:

   if( fHandle == INVALID_HANDLE_VALUE )
   {
     return;
   }

And this may solve your other problem:

else if( file_data.dwFileAttributes != FILE_ATTRIBUTE_HIDDEN && 
   file_data.dwFileAttributes != FILE_ATTRIBUTE_SYSTEM  &&
   wcscmp(file_data.cFileName, L".") != 0 && 
   wcscmp(file_data.cFileName, L"..") != 0
 )
{
    results << wrkdir << "\\" << file_data.cFileName << endl;
}

Also see @fretje's answer as well. It gives another problem that your code has.

Updated new: You need to use fHandle as a local variable as well, not global variable.

Change to:

 HANDLE fHandle = FindFirstFile( temp.c_str(), &file_data );
like image 70
Brian R. Bondy Avatar answered Sep 11 '26 10:09

Brian R. Bondy


You are changing the value of your local wrkdir variable:

wrkdir = wrkdir + L"\\" + file_data.cFileName;
find_files( wrkdir );

I think you have to call find_files there like this:

find_files( wrkdir + L"\\" + file_data.cFileName );

and not change the value of wrkdir.

like image 33
fretje Avatar answered Sep 11 '26 12:09

fretje



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!