tags:

views:

140

answers:

4

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??

+3  A: 

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
Thanks dude.. It works. :D
ganuke
A: 

Take a look at this example from MSDN using CFileFind.

Naveen
+2  A: 

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.

BennyG
A: 

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