zoukankan      html  css  js  c++  java
  • Windows下文件检索的基本姿势

    ##要点

    使用FindFirstFileFindNextFile两个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;
    }
    

    ##运行截图

    运行截图

  • 相关阅读:
    苹果全球营销高级副总裁Phil Schiller曾考虑炒掉长期创意代理商Media Arts Lab
    Amazon Seller Central is Temporarily Unavailable
    三星高管:我们手机卖的好是因为营销成功
    欧洲跨境电商增速将达21% 德国力压群雄
    苹果挖走了亚马逊搜索技术副总裁,或为提升应用商店搜索功能
    亚马逊CEO贝索斯致股东信:阐述公司未来计划
    Amazon.com 购物 信用卡预售期
    佛论婆媳关系
    Focalprice李培亮:梦想让人在我店里排队
    DX孟虎点评新兴市场:巴西俄罗斯火爆背后
  • 原文地址:https://www.cnblogs.com/csnd/p/12897020.html
Copyright © 2011-2022 走看看