开发者

Searching by extensions in specific Dir C++

开发者 https://www.devze.com 2023-01-17 08:38 出处:网络
In specific directory i need to find all files with some specific extension (for example .log) and than save it somewhere. In need only c++ solutions cause i\'m trying it on win 2003 server and c++ is

In specific directory i need to find all files with some specific extension (for example .log) and than save it somewhere. In need only c++ solutions cause i'm trying it on win 2003 server and c++ is my restrictions. Thank you very munc开发者_运维知识库h


You can enumerate all of the files in a directory using FindFirstFile and FindNextFile (and don't forget to call FindClose when you're done). You can pass in a filter to these functions to only look for certain filenames, e.g. directory\*.log.

As you enumerate the files, the WIN32_FIND_DATA structure that gets returned tells you the filename and attributes of each file (among other things). For each file, check the file attributes to make sure it's a regular file by checking that it has the FILE_ATTRIBUTE_NORMAL flag so that you ignore directories.

For example:

WIN32_FIND_DATA fileInfo;
HANDLE hFind = FindFirstFile("C:\\directory\\to\\search\\*.log", &fileInfo);
if(hFind == INVALID_HANDLE_VALUE)
    ;  // handle error
else
{
    do
    {
        if(fileInfo.dwFileAttributes & FILE_ATTRIBUTE_NORMAL)
        {
            printf("Found a .log file: %s\n", fileInfo.cFileName);
        }
    } while(FindNextFile(hFind, &fileInfo));
    if(GetLastError() != ERROR_NO_MORE_FILES)
        ;  // handle error
    FindClose(hFind);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消