#include <windows.h>
#include <iostream>
#include <string>
using namespace Std;
only directories can be processed : Lppath can only be paths
void Find (char *lppath)
{
Char Szfind[max_path];
Char Szfile[max_path];
Win32_find_data Findfiledata;
strcpy (Szfind,lppath);
strcat (Szfind, "//*.*");
HANDLE Hfind=::findfirstfile (Szfind,&findfiledata);
if (Invalid_handle_value = = hfind) return;
while (TRUE)
{
if (Findfiledata.dwfileattributes & File_attribute_directory)
{
if (findfiledata.cfilename[0]!= '. ')
{
strcpy (Szfile,lppath);
strcat (Szfile, "//");
strcat (Szfile,findfiledata.cfilename);
Find (Szfile);
}
}
Else
{
cout<<findfiledata.cfilename<<endl;
}
if (! FindNextFile (Hfind,&findfiledata))
Break
}
FindClose (hfind);
}
directories and files can be processed at the same time :p ath can be a path, a file name, or a file wildcard
void _find (string path) {
take the path name before the last "//" section , including "//"
String Prefix=path.substr (0,path.find_last_of ('//') +1);
Win32_find_data Findfiledata;
HANDLE Hfind=::findfirstfile (Path.c_str (), &findfiledata);
if (Invalid_handle_value = = hfind)
{
cout<< " file wildcard error " <<endl;
Return
}
while (TRUE)
{
Catalogue
if (Findfiledata.dwfileattributes & File_attribute_directory)
{
is not the current directory, nor is it a parent directory
if (findfiledata.cfilename[0]!= '. ')
{
Find Next Level directory
_find (prefix+findfiledata.cfilename+ "//" + "* *");
}
}
file
Else
{
cout<<findfiledata.cfilename<<endl;
}
if (! FindNextFile (Hfind,&findfiledata))
Break
}
FindClose (hfind);
}
void Main () {
Find ("E:");// directory:E - Drive
_find ("E://*.*");//e all files under disk
String str= "./test"; The directory is specified here
string path;
cout<< " Please enter a file wildcard:" <<flush;
cin>>path;
Str=str+path;
Find ((char*) str.c_str ());// can handle "." , ".." can not handle "*" , ".. //*"
_find (str);// can handle "*",".. * " cannot be processed ". " , ".."
System ("pause");
}
Copy Code
There is a struct, which is a C + + Self-contained, is this:
typedef struct _win32_find_data{
DWORD dwfileattributes;// file Properties
FILETIME ftcreationtime;// File creation Time
FILETIME ftlastaccesstime;// file last access time
FILETIME ftlastwritetime;// last modified time of file
DWORD Nfilesizehigh;// High file length + bit
DWORD nfilesizelow;// Low file length + bit
DWORD dwReserved0;// System Retention
DWORD dwReserved1;// System Retention
TCHAR cfilename[MAX_PATH];// Long File name
TCHAR calternatefilename[];// Optional name of the file
} win32_find_data;
Win32 C + + Traversal directory and File source code