Advertisement
2_2002-2004 Files #126959

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

สรุปโดย 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.

ซอร์สโค้ด
original-source
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);	
	}
Upload
Basic File Functions
---------------------
Read File:
----------
Imports System
Imports System.IO
Public Module modmain
  Sub Main()
   Dim srFile as StreamReader = File.OpenText("C:\autoexec.bat")
   Console.WriteLine(srFile.ReadToEnd())
   srFile.Close
  End Sub
End Module
Write File:
-----------
Imports System
Imports System.IO
Public Module modmain
  Sub Main()
   Dim swFile as StreamWriter = File.CreateText("C:\test.txt")
   swFile.WriteLine("Hello World ;)")
   swFile.Flush()
   swFile.Close()
  End Sub
End Module
File Version Info:
------------------
Imports System
Imports System.Diagnostics
Public Module modmain
  Sub Main()
   Dim versionInfo As FileVersionInfo = FileVersionInfo.GetVersionInfo("C:\WINNT\Notepad.exe")
   Console.WriteLine("Notepad version " + versionInfo.FileVersion)
   Console.WriteLine("Description: " + versionInfo.FileDescription)
  End Sub
End Module
File Size:
----------
Imports System
Imports System.IO
Public Module modmain
  Sub Main()
   Dim MyFile as new FileInfo ("c:\autoexec.bat")
   Console.WriteLine("The length of autoexec.bat is " _
     + MyFile.Length.ToString + " bytes.")
  End Sub
End Module
Copy Move and Delete File:
--------------------------
Imports System
Imports System.IO
Public Module modmain
  Sub Main()
   	File.Copy("c:\autoexec.bat", "c:\autocopy.ext")
   	File.Move("c:\autocopy.ext", "c:\newname.ext")
	File.Delete("c:\autocopy.ext")	
  End Sub
End Module
Temporary File Name:
---------------------
The GetTempFileName function creates a name for a temporary file. The filename is the concatenation of specified path and prefix strings, a hexadecimal string formed from a specified integer, and the .TMP extension.
Imports System
Imports System.IO
Public Module modmain
  Sub Main()
   Console.WriteLine(Path.GetTempFileName)
  End Sub
End Module
ความคิดเห็นดั้งเดิม (3)
กู้คืนจาก Wayback Machine