How to get list of files with a specific extension in a given folder?

前端 未结 6 1918
孤街浪徒
孤街浪徒 2020-11-27 03:29

I want to get the file names of all files that have a specific extension in a given folder (and recursively, its subfolders). That is, the file name (and extension), not th

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 04:18

    Get list of files and process each file and loop through them and store back in different folder

    void getFilesList(string filePath,string extension, vector & returnFileName)
    {
        WIN32_FIND_DATA fileInfo;
        HANDLE hFind;   
        string  fullPath = filePath + extension;
        hFind = FindFirstFile(fullPath.c_str(), &fileInfo);
        if (hFind != INVALID_HANDLE_VALUE){
            returnFileName.push_back(filePath+fileInfo.cFileName);
            while (FindNextFile(hFind, &fileInfo) != 0){
                returnFileName.push_back(filePath+fileInfo.cFileName);
            }
        }
    }
    

    USE: you can use like this load all the files from folder and loop through one by one

    String optfileName ="";        
    String inputFolderPath =""; 
    String extension = "*.jpg*";
    getFilesList(inputFolderPath,extension,filesPaths);
    vector::const_iterator it = filesPaths.begin();
    while( it != filesPaths.end())
    {
        frame = imread(*it);//read file names
            //doyourwork here ( frame );
        sprintf(buf, "%s/Out/%d.jpg", optfileName.c_str(),it->c_str());
        imwrite(buf,frame);   
        it++;
    }
    

提交回复
热议问题