http://www.developer.com/net/cplus/article.php/3499331/Search-Entire-Directories-for-Specified-Files-with-Managed-C.htm
I recently designed and coded a popular anti-malware (spyware) application that, amongst other tasks, searched for known malware "fingerprints" in memory, on disk, and in the Windows Registry. To search for the files on disk, I wrote a recursive function that searches a given file starting at a specified folder. The function then calls itself for each folder it finds until it has searched the entire folder hierarchy. This week's column illustrates this function.
The DirectoryInfo class is very easy to use and, for this particular task, requires the knowledge of only a few methods and properties:
As you can see in the following code, the FileFind function first retrieves the DirectoryInfo object for the specified directory (startingDir):
Once the DirectoryInfo::Exists property is checked to ensure that the directory exists, the directory's files are retrieved into a FileInfo array. This array is enumerated and each file's name is compared to the caller's specified file name. If the file names match, the FileInfo object is added to the ArrayList, which the caller will ultimately enumerate when the function has completed. (For the anti-malware application, I had to also compare MD5 hash-code values when a matching file name was found. However, as most people don't need this added functionality, the FindFile function matches only on file name.)
Once all the files have been processed for the current directory, the sub-directories are retrieved and the FindFile function is then called for each of these sub-directories. This process enables the FindFile function to correctly search an entire directory hierarchy for all instances of the caller's specified file name.
Now, all the caller has to do is to instantiate an ArrayList object, pass it to the FindFile function, and then enumerate the ArrayList object upon return:
Tom Archer owns his own training company, Archer Consulting Group, which specializes in educating and mentoring .NET programmers and providing project management consulting. If you would like to find out how the Archer Consulting Group can help you reduce development costs, get your software to market faster, and increase product revenue, contact Tom through his Web site.
Search Entire Directories for Specified Files with Managed C++
April 22, 2005
Enumerating Directories and Files with .NET
An MFC developer would enumerate directories in search of specific files typically via the CFindFile class or by using the Win32 FindFirstFile and FindNextFile functions. In .NET, this task is accomplished via the DirectoryInfo and FileInfo classes.
Generic Function to Search for Files
Now that you know the basics of the .NET classes needed to enumerate directories and files, you can easily write a generic function to search a given directory path for a specified file. To that end, the FindFile function takes the following parameter:
void FindFile(String* startingDir, String* fileName, ArrayList* foundFiles)
{
DirectoryInfo* dirInfo = new DirectoryInfo(startingDir);
if (dirInfo->Exists) // make sure directory exists
{
FileInfo* files[] = dirInfo->GetFiles();
for (int i = 0; i < files->Length; i++)
{
String* currFileName = files[i]->ToString();
if (0 == String::Compare(currFileName, fileName, true))
{
foundFiles->Add(files[i]);
}
}
DirectoryInfo* subDirs[] = dirInfo->GetDirectories();
for (int i = 0; i < subDirs->Length; i++)
{
String* dirName = subDirs[i]->FullName;
FindFile(dirName, fileName, foundFiles);
}
}
}
ArrayList* foundFiles = new ArrayList();
FindFile(S"d:\\acg\\legal", S"eula.txt", foundFiles);
if (foundFiles->Count)
{
for (int i = 0; i < foundFiles->Count; i++)
{
FileInfo* fileInfo =
static_cast<FileInfo*>(foundFiles->get_Item(i));
Console::WriteLine(fileInfo->FullName);
}
}
else
{
Console::WriteLine("SNo matching files found");
}
About the Author