I needed to search for a single file, in a particular directory. Although there are plenty of examples, most examples are pretty big to just find a file. So i decided to make my own. Im posting it since i am not shure if this done the right way. Would this work an all win versions? For every user?? If its correct, i hope other might be able to use it.
you use it like this:Code:// searches recursive for fileName in dir and if fileName is found, the fullpath is copied into fileName bool SearchFile(char dir[FILENAMESIZE],char fileName[FILENAMESIZE]) { WIN32_FIND_DATA fd; char txt[FILENAMESIZE]; sprintf(txt,"%s%s",dir,fileName); HANDLE hFind = FindFirstFile(txt,&fd); FindClose(hFind); if(hFind != INVALID_HANDLE_VALUE) { sprintf(fileName,"%s%s",dir,fd.cFileName); return true; } sprintf(txt,"%s*",dir); hFind = FindFirstFile(txt,&fd); while(FindNextFile(hFind,&fd)) { if((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && strcmp(fd.cFileName,"..") != 0) { sprintf(txt,"%s%s\\",dir,fd.cFileName); if(SearchFile(txt,fileName)) { FindClose(hFind); return true; } } } FindClose(hFind); return false; }
Code:void FindSomeFile() { char dir[FILENAMESIZE]; char fileName[FILENAMESIZE]; strcpy(dir,"C:\\"); strcpy(fileName,"p*.exe"); if(SearchFile(dir,fileName)) {showMsg(fileName);} else {} }



LinkBack URL
About LinkBacks


