Copy codeThe Code is as follows: # include <stdio. h>
# Include <windows. h>
Void FindFile (char *, char *);
Int count = 0; // number of statistical files
Char fname [32];
# Define BUFSIZE 256
Int main (int argc, char * argv [])
{
Char szLogicalDriveStrings [BUFSIZE];
DWORD iLength;
Int iSub;
Printf ("enter the name of the file to be searched :");
Scanf ("% s", fname );
ZeroMemory (szLogicalDriveStrings, BUFSIZE );
ILength = GetLogicalDriveStringsA (BUFSIZE-1, szLogicalDriveStrings );
For (iSub = 0; iSub <iLength; iSub + = 4)
{
// If it is not a fixed disk drive: local hard disk or mobile hard disk, ignore
If (GetDriveType (szLogicalDriveStrings + iSub )! = 3)
Continue;
FindFile (szLogicalDriveStrings + iSub ,"*.*");
}
Printf ("% d files found in total... \ n", count );
Scanf ("% * d ");
Return 0;
}
Void FindFile (char * pfilename, char * pfilter)
{
WIN32_FIND_DATA findfiledate;
HANDLE hfind;
Char filename [512];
Char lpFileName [512];
Char _ lpFileName [512];
Int I;
Int result;
For (I = 0; * (pfilename + I )! = '\ 0'; I ++)
Filename [I] = * (pfilename + I );
Filename [I] = '\ 0 ';
// If the last character is not '\'
If (filename [strlen (filename)-1]! = '\\')
Strcat (filename, "\"); // Add '\'
Strcpy (lpFileName, filename );
Strcat (lpFileName, pfilter );
Hfind = FindFirstFile (lpFileName, & findfiledate );
If (hfind = INVALID_HANDLE_VALUE)
Return;
Do
{
// If it is not a directory
If (! (Findfiledate. dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ))
{
// If the specified file is found
If (0 = strcmp (fname, findfiledate. cFileName ))
{
Printf ("% s \ n", filename, findfiledate. cFileName );
Count ++;
}
}
// If it is a directory
Else
{
// Do not output
If (findfiledate. cFileName [0]! = '.')
{
Strcpy (_ lpFileName, filename );
Strcat (_ lpFileName, findfiledate. cFileName );
FindFile (_ lpFileName, pfilter); // recursive
}
}
} While (FindNextFile (hfind, & findfiledate); // FindNextFile returns true and continues searching
FindClose (hfind );
Return;
}
Using Recursion to search for files is less efficient, and multithreading is better.