##要点
使用FindFirstFile和FindNextFile两个WindowsAPI,并配合链表或队列存储文件夹序列。
##C++源码(链表存储)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <Windows.h>
using namespace std;
typedef struct DirTable
{
CHAR dir[255];
DirTable* next;
}DirTable;
DirTable *g_firstNode = NULL, *g_lastNode = NULL, *g_iterNode = NULL;
int g_reCount = 0;
void AddNode(const CHAR* dirRoad)
{
DirTable* NewNode = new DirTable;
ZeroMemory(NewNode->dir, 255);
strcpy_s(NewNode->dir, 255, dirRoad);
NewNode->next = NULL;
if (g_firstNode == NULL)
{
g_firstNode = NewNode;
g_iterNode = NewNode;
g_lastNode = NewNode;
}
else {
g_iterNode->next = NewNode;
g_iterNode = g_iterNode->next;
g_lastNode = NewNode;
}
return;
}
void FindFile(const CHAR* rootId, const CHAR* TargetFile)
{
CHAR dirRoad[255] = { 0 }; //链表中添加的文件夹路径
CHAR targetFileRoad[255] = { 0 }; //目标文件路径
CHAR searchRoad[255] = { 0 }; //文件夹搜索路径
HANDLE h_File = NULL;
WIN32_FIND_DATA winData;
strcat_s(searchRoad, 255, rootId);
strcat_s(searchRoad, 255, "\*.*");
h_File = FindFirstFile(searchRoad, &winData);
do {
if (winData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
{
if (strcmp(winData.cFileName, ".") == 0 || strcmp(winData.cFileName, "..") == 0)
{
continue;
}
strcpy_s(dirRoad, 255, rootId);
strcat_s(dirRoad, 255, "\");
strcat_s(dirRoad, 255, winData.cFileName);
AddNode(dirRoad);
ZeroMemory(dirRoad, 255);
}
} while (FindNextFile(h_File, &winData));
strcat_s(targetFileRoad, 255, rootId);
strcat_s(targetFileRoad, 255, "\");
strcat_s(targetFileRoad, 255, TargetFile);
h_File = FindFirstFile(targetFileRoad, &winData);
if (h_File != INVALID_HANDLE_VALUE)
{
do
{
g_reCount++;
printf("
Result %d ==> %s
", g_reCount, targetFileRoad);
} while (FindNextFile(h_File, &winData));
}
ZeroMemory(targetFileRoad, 255);
return;
}
void SearchFile(const CHAR* rootId, const CHAR* TargetFile)
{
FindFile(rootId, TargetFile);
while (g_firstNode)
{
FindFile(g_firstNode->dir, TargetFile);
g_firstNode = g_firstNode->next;
}
if (!g_firstNode) printf("
Searching End >_<...
Total-Result: ==> Find %d Files...
", g_reCount);
return;
}
int main(void)
{
CHAR* strTargetFile = (CHAR*)malloc(255 * sizeof(CHAR));
CHAR* strrootId = (CHAR*)malloc(255 * sizeof(CHAR));
printf("Please input TargetFile:");
scanf_s("%s", strTargetFile, 255);
printf("Please input rootId:");
scanf_s("%s", strrootId, 255);
SearchFile(strrootId, strTargetFile);
system("pause");
return 0;
}
##C++源码(队列存储)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <Windows.h>
#include <queue>
using namespace std;
queue<string> g_fileDirectoryQuery;
int g_reCount = 0;
void FindFile(const string rootId, const string &TargetFile)
{
HANDLE h_File = NULL;
WIN32_FIND_DATA winData;
string dirRoad = ""; //添加进队列的文件夹路径
string targetFileRoad = ""; //目标文件路径
dirRoad.append(rootId);
dirRoad.append("\*.*");
h_File = FindFirstFile(dirRoad.c_str(), &winData);
dirRoad = "";
do {
if (winData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
{
if (strcmp(winData.cFileName, ".") == 0 || strcmp(winData.cFileName, "..") == 0) continue;
dirRoad.append(rootId);
dirRoad.append("\");
dirRoad.append(winData.cFileName);
g_fileDirectoryQuery.push(dirRoad);
dirRoad = "";
}
} while (FindNextFile(h_File, &winData));
targetFileRoad.append(rootId);
targetFileRoad.append("\");
targetFileRoad.append(TargetFile);
h_File = FindFirstFile(targetFileRoad.c_str(), &winData);
if (h_File != INVALID_HANDLE_VALUE)
{
do
{
g_reCount++;
cout << endl << "Result No." << g_reCount << " ==> " << targetFileRoad << endl;
} while (FindNextFile(h_File, &winData));
}
return;
}
void SearchFile(const string rootId, const string &TargetFile)
{
FindFile(rootId, TargetFile);
while (!g_fileDirectoryQuery.empty())
{
FindFile(g_fileDirectoryQuery.front(), TargetFile);
g_fileDirectoryQuery.pop();
}
if (g_fileDirectoryQuery.empty()) cout << "
Searching End >_<...
Total-Result ==> Find " << g_reCount << " Files !
" << endl;
return;
}
int main(void)
{
string strTargetFile = "";
string strRootId = "";
printf("Please input TargetFile:");
cin >> strTargetFile;
printf("Please input rootId:");
cin >> strRootId;
SearchFile(strRootId, strTargetFile);
system("pause");
return 0;
}
##运行截图