How to search a directory tree for files
The following code sample illustrates how to search from a given directory downward through the entire directory tree. The sample output from this example is directed to the system debug screen. For this example, the first class member (below) is called by a menu item. After the search is finished, a message box pops up. http://www.concentric.net/~cgalbrai/dirsrh.shtml
AI
एआई सारांश: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
सोर्स कोड
void CTestView::OnSearch()
{
// szFilename is declared in the header as array of char
// look for MyFile.txt (or whatever)
strcpy(szFilename,"MyFile.txt");
// go to root directory (or to whichever directory that you wish)
_chdir("C:\\");
// search for the filename
SearchDirectory();
// announce when done
MessageBox("Done Searching");
}
SearchDirectory() is called initially from OnSearch(). SearchDirectory() is then called recursively (from itself) until the end of the directory tree is reached and all branches are searched.
void CTestView::SearchDirectory()
{
struct _finddata_t filestruct;
long hnd;
char buffer[_MAX_PATH];
// set _findfirst to find everthing
hnd = _findfirst("*",&filestruct);
// if handle fails, drive is empty...
if((hnd == -1)) return;
// get first entity on drive - check if it's a directory
if(::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_DIRECTORY
&& !(::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_HIDDEN)) {
// if so, change to that directory and recursively call SearchDirectory
if(*filestruct.name != '.') {
_chdir(filestruct.name);
SearchDirectory();
// go back up one directory level
_chdir("..");
}
}
else {
// if it's not a directory and it matches what you want...
if(!stricmp(filestruct.name,szFilename)) {
// output the filename with path to debugger
_getcwd(buffer,_MAX_PATH);
strcat(buffer,"\\");
strcat(buffer,filestruct.name);
strcat(buffer,"\r\n");
OutputDebugString(buffer);
}
}
while(!(_findnext(hnd,&filestruct))) {
if(::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_DIRECTORY
&& !(::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_HIDDEN)) {
if(*filestruct.name != '.') {
_chdir(filestruct.name);
SearchDirectory();
_chdir("..");
}
}
else {
if(!stricmp(filestruct.name,szFilename)) {
_getcwd(buffer,_MAX_PATH);
strcat(buffer,"\\");
strcat(buffer,filestruct.name);
strcat(buffer,"\r\n");
OutputDebugString(buffer);
}
}
}
_findclose(hnd);
}
मूल टिप्पणियाँ (3)
Wayback Machine से पुनर्प्राप्त