优化后的Traverse.cpp:
/*---- Includes ------------------------*/
#include "pch.h"
#include "windows.h"
#include <stdio.h>
#include "Traverse.h"
/*---- Static Function Statements ------------------------*/
/*
*@brief 遍历搜索[target_dir]路径下的目标文件,并对符合条件的文件执行[my_func]操作
*@param input: type: 1 查找[target_dir]下一级子目录中文件名为[target_format]的文件
2 查找[target_dir]目录中文件名为[target_format]的文件
3 遍历搜索[target_dir]路径下的所有文件和目录
target_dir: 目标搜索路径(绝对路径)
show: 0不打印搜索详情 1打印搜索详情
my_func: 对文件进行的操作
output: sum[0]: [target_dir]中所有目录个数
sum[1]: [target_dir]中所有文件个数
*/
static void TraverseDir(int type, char *target_dir, char *target_format, int show, int *sum, file_operation my_func)
{
HANDLE file;
WIN32_FIND_DATAA file_data;
WIN32_FIND_DATA pNextInfo;
char file_dir[1024];
memset(file_dir, 0x00, sizeof(file_dir));
char sub_dir[1024];
char wcharTochar_str[1024];
bool attributes_match = false;
if ((1 == type) || (3 == type))
{
sprintf(file_dir, "%s\\*", target_dir);
if (1 == show)
{
printf("%s\n", target_dir);
}
}
else if (2 == type)
{
sprintf(file_dir, "%s\\%s", target_dir, target_format);
if (1 == show)
{
printf("%s\n", file_dir);
}
}
file = FindFirstFileA(file_dir, &file_data);
if (file == INVALID_HANDLE_VALUE)
{
//printf("搜索文件失败\n");
}
else
{
if (file_data.cFileName[0] != '.')
{
if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
attributes_match = true;
if (type & 0x01)
{
if (show)
{
for (int x = 0; x < show; x++)
{
printf("└---");