Search instance code of a specified file in C Language

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.