Read all files inside a folder including files in

2019-08-31 15:06发布

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

标签: c++ winapi
4条回答
相关推荐>>
2楼-- · 2019-08-31 15:43

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)

查看更多
等我变得足够好
3楼-- · 2019-08-31 15:46

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.

查看更多
Bombasti
4楼-- · 2019-08-31 15:47

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
查看更多
5楼-- · 2019-08-31 15:59

Take a look at this example from MSDN using CFileFind.

查看更多
登录 后发表回答