VB icon

How to search a directory tree for files

Email
Submitted on: 1/1/2015 1:10:00 AM
By: Found on the World Wide Web (from psc cd)  
Level: Not Given
User Rating: By 4 Users
Compatibility: C++ (general)
Views: 1275
 
     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
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: How to search a directory tree for files
// Description: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
// By: Found on the World Wide Web (from psc cd)
//**************************************

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);	
	}


Other 18 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.