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
  • 338,529
  • 73
  • 897
  • 968
ganuke
  • 4,605
  • 5
  • 19
  • 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
  • 117,899
  • 60
  • 232
  • 319
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,282
  • 8
  • 11
0

Take a look at this example from MSDN using CFileFind.

Naveen
  • 71,879
  • 44
  • 171
  • 229
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
  • 3,862
  • 1
  • 18
  • 21