c++ - find files in whole hard disk -
this question has answer here:
i have program searches files of particular extension in specific drive (i using windows), let it's "c:\" , print these files on console. want generalize program. want program search in partitions (drives) .. how can this.. function should use..here code sample
int searchdirectory(std::vector<std::string> &refvecfiles, const std::string &refcstrrootdirectory, const std::string &refcstrextension, bool bsearchsubdirectories = true) { std::string strfilepath; // filepath std::string strpattern; // pattern std::string strextension; // extension handle hfile; // handle file win32_find_data fileinformation; // file information strpattern = refcstrrootdirectory + "\\*.*"; hfile = ::findfirstfile(strpattern.c_str(), &fileinformation); if(hfile != invalid_handle_value) { { if(fileinformation.cfilename[0] != '.') { strfilepath.erase(); strfilepath = refcstrrootdirectory + "\\" + fileinformation.cfilename; if(fileinformation.dwfileattributes & file_attribute_directory) { if(bsearchsubdirectories) { // search subdirectory int irc = searchdirectory(refvecfiles, strfilepath, refcstrextension, bsearchsubdirectories); if(irc) return irc; } } else { // check extension strextension = fileinformation.cfilename; strextension = strextension.substr(strextension.rfind(".") + 1); if(strextension == refcstrextension) { // save filename refvecfiles.push_back(strfilepath); } } } } while(::findnextfile(hfile, &fileinformation) == true); // close handle ::findclose(hfile); dword dwerror = ::getlasterror(); if(dwerror != error_no_more_files) return dwerror; } return 0; } int main() { int irc = 0; std::vector<std::string> vecavifiles; std::vector<std::string> vectxtfiles; // search 'c:' '.avi' files including subdirectories irc = searchdirectory(vecavifiles, "c:", "apk"); if(irc) { std::cout << "error " << irc << std::endl; return -1; } // print results for(std::vector<std::string>::iterator iteravi = vecavifiles.begin(); iteravi != vecavifiles.end(); ++iteravi) std::cout << *iteravi << std::endl; // search 'c:\textfiles' '.txt' files excluding subdirectories irc = searchdirectory(vectxtfiles, "c:", "txt", false); if(irc) { std::cout << "error " << irc << std::endl; return -1; } // print results for(std::vector<std::string>::iterator itertxt = vectxtfiles.begin(); itertxt != vectxtfiles.end(); ++itertxt) std::cout << *itertxt << std::endl; // wait keystroke _getch(); return 0; }
i'm no windows programmer looks have find out drives available in system , call function of them. see here how list of available drives on windows: enumerating available drive letters in windows
Comments
Post a Comment