0

I want to read all files inside a given folder(path to folder) using FindFirstFile method provide in windows API. Currently I'm only succeeded in reading files inside the given folder. I could not read files inside sub folders. Can anyone help me to do this??

James McNellis
  • 348,265
  • 75
  • 913
  • 977
ganuke
  • 4,915
  • 5
  • 20
  • 12

4 Answers4

3

When you call FindFirstFile/FindNextFile, some of the "files" it returns will actually be directories. You can check if something is a directory or not by looking at the dwFileAttributes field of the WIN32_FIND_DATA structure that gets returned to you.

If you find one that is a directory, then you can simply call your file finding function recursively to go into the subfolders.

Note: Make sure to put in a special case for the . and .. psuedo-directories, otherwise your function will recurse into itself and you'll get a stack overflow

Here's the documentation if you haven't already found it:

FindFirstFile

WIN32_FIND_DATA

possible values for dwFileAttributes (remember these are all bit flags, so you'll have to use & to check)

Orion Edwards
  • 121,657
  • 64
  • 239
  • 328
2

Alternatively, you can use boost::filesystem which will not only give you a clean API, but will also make your code portable on all supported platforms.

BenG
  • 1,292
  • 8
  • 11
0

Take a look at this example from MSDN using CFileFind.

Naveen
  • 74,600
  • 47
  • 176
  • 233
0

I've used this code to read the files in the specified directory.

CFileFind finder;

BOOL bWorking = finder.FindFile( directory );

while( bWorking )
{
    bWorking = finder.FindNextFile();                   
}//end while
Justin
  • 4,002
  • 2
  • 19
  • 21