The program iterates through all the files in the folder and its subfolders and outputs them to the standard output stream or file stream.
1. Consider traversing all files in a single-level directory. Take C:\WINDOWS as an example:
A pointer to a data structure _finddata_t, a file information struct.
struct _finddata_t
{
unsigned attrib; File properties
time_t time_create; File creation time
time_t time_access; File last access time
time_t Time_write; Last modification time of the file
_fsize_t size; Number of File bytes
Char Name[_max_fname]; Filename
The file attributes are unsigned integers, and the values are the corresponding macros: _a_arch (archive), _a_subdir (folder), _a_hidden (hidden), _a_system (System), _a_normal (normal), _a_rdonly (read-only). It is easy to see that through this structure, we can get a lot of information about the file. By combining the following functions, we can store the file information in this struct:
Match the first file of the current directory by the filename naming rule
Match the current directory to the next file by the filename naming rule
_findnext (_in_ intptr_t _findhandle, _out_ struct _finddata64i32_t * _finddata);
Closes the file handle returned by _findfirst
_findclose (_in_ intptr_t _findhandle);
The _findfirst function returns a handle to the file, with a data type of long. The traversal process can specify the file type, which is implemented by the assignment of the filename, for example, to traverse all the. exe files under C:\WINDOWS
BOOL Transfer (String fileName = "C:\\windows\\*.exe", int exenum = 0)
{
_finddata_t FileInfo;
Long handle = _findfirst (Filename.c_str (), &fileinfo);
if (handle = = -1l)
{
Cerr << "Failed to transfer files" << Endl;
return false;
}
Do
{
Exenum + +;
cout << Fileinfo.name <<endl;
} while (_findnext (handle, &fileinfo) = = 0);
cout << ". exe files ' number:" << exenum << Endl;
return true;
}
BOOL Transfer (String fileName = "C:\\windows\\*.exe", int exenum = 0)
{
_finddata_t FileInfo;
Long handle = _findfirst (Filename.c_str (), &fileinfo);
if (handle = = -1l)
{
Cerr << "Failed to transfer files" << Endl;
return false;
}
{
Exenum + +;
cout << Fileinfo.name <<endl;
} while (_findnext (handle, &fileinfo) = = 0);
cout << ". exe files ' number: " << exenum << Endl;
return true;
}
2. Traverse all files under the folder and its subfolders. The folder directory in the operating system is a tree structure that uses a deep search strategy to traverse all files. Using the _a_subdir property, you can run the following program:
void Dfsfolder (String folderpath, Ofstream &fout)
{
_finddata_t FileInfo;
string strfind = FolderPath + "\\*";
Long Handle = _findfirst (Strfind.c_str (), &fileinfo);
if (Handle = = -1l)
{
cerr << "can not match the folder path" << Endl;
exit ( -1);
}
do{
//Determine if there is a subdirectory
if (Fileinfo.attrib & _a_subdir)
{
//This statement is important
if (strcmp (Fileinfo.name, ".")! = 0) && (strcmp (Fileinfo.name, "...")! = 0))
{
string NewPath = FolderPath + "\ \" + Fileinfo.name;
Dfsfolder (NewPath, fout);
}
}
Else
{
fout << folderpath << "\ \" << fileinfo.name << "";
}
}while (_findnext (Handle, &fileinfo) = = 0);
_findclose (Handle);
Fout.close ();
}
void Dfsfolder (String folderpath, Ofstream &fout)
{
_finddata_t FileInfo;
String strfind = FolderPath + "\\*";
Long Handle = _findfirst (Strfind.c_str (), &fileinfo);
if (Handle = = -1l)
{
Cerr << "can not match the folder path" << Endl;
Exit (-1);
}
do{
//Determine if there are subdirectories
if (Fileinfo.attrib & _a_subdir)
{
This statement is important
if (strcmp (Fileinfo.name, ".")! = 0) && (strcmp (Fileinfo.name, "...")! = 0))
{
String newpath = FolderPath + "\ \" + Fileinfo.name;
Dfsfolder (NewPath, fout);
}
}
Else
{
Fout << folderpath << "\ \" <&L T Fileinfo.name << "";
}
}while (_findnext (Handle, &fileinfo) = = 0);
_findclose (Handle);
Fout.close ();
}
In the If branch where there is no subdirectory, as the system enters a subdirectory, the first two files (clips) that match are "." (current directory), "..." (Previous level directory). You need to ignore both of these situations. When you need to process a traversed file, add the appropriate code to the Else branch.
Traverse folder under C + +