要点

使用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("\nResult %d ==> %s\n", 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("\n\nSearching End >_<...\n\nTotal-Result: ==> Find %d Files...\n\n", 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 << "\n\nSearching End >_<...\n\nTotal-Result ==> Find " << g_reCount << " Files !\n" << 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;
}

运行截图

Windows下文件检索的基本姿势的更多相关文章

  1. 转:windows下命令行工具

    转自: http://www.cnblogs.com/haochuang/p/5593411.html Windows下CMD不好用,远没有Linux,或者一些SSH工具用起来方便.其实Windows ...

  2. windows下使用体验更好的控制台——ConsoleZ

    转做前端开发以来,每天使用最频繁的工具就是控制台了,git提交代码要用,npm安装node包也要用,grunt task 也要用,可是系统自带的cmd太难用了, 那么问题就来了: "wind ...

  3. [爬虫]Windows下如何安装python第三方库lxml

    lxml是个非常有用的python库,它可以灵活高效地解析xml与BeautifulSoup.requests结合,是编写爬虫的标准姿势. 但是,当lxml遇上Windows,简直是个巨坑.掉在安装陷 ...

  4. [转]Linux/Windows下脚本对拍程序

    [新]简单写法 (转载自:https://blog.csdn.net/ylsoi/article/details/79824655) 要求:文件输入输出,且输入输出文件需要对应 Linux: #inc ...

  5. windows下sqli-labs的搭建及学习(GET篇)

    环境搭建: 源码下载地址:https://github.com/Audi-1/sqli-labs 需要搭建以下环境: apache+mysql+php Tomcat+mysql+java(部分关卡需要 ...

  6. Windows下如何安装python第三方库lxml

    lxml是个非常有用的python库,它可以灵活高效地解析xml,与BeautifulSoup.requests结合,是编写爬虫的标准姿势. 参考 Windows下如何安装python第三方库lxml ...

  7. 在windows下安装gulp —— 基于 Gulp 的前端集成解决方案(一)

    相关连接导航 在windows下安装gulp —— 基于 Gulp 的前端集成解决方案(一) 执行 $Gulp 时发生了什么 —— 基于 Gulp 的前端集成解决方案(二) 常用 Gulp 插件汇总 ...

  8. 让 windows 下的命令行程序 cmd.exe 用起来更顺手

    在 Windows 下使用 Larave 框架做开发,从 Composer 到 artisan 总是避免不了和 cmd.exe 打交道,系统默认的命令行界面却是不怎么好看,且每行显示的字符数是做了限制 ...

  9. Windows下Visual studio 2013 编译 Audacity

    编译的Audacity版本为2.1.2,由于实在windows下编译,其源代码可以从Github上取得 git clone https://github.com/audacity/audacity. ...

随机推荐

  1. 传统项目转前端工程化——路由跳转时出现浏览器锁死和白屏【该死的同步ajax】

    [一开始我想到是该死的同步ajax,但我没验证,把他忽略了] 在探索前端工程化vue-cli做spa时,从搜索结果页跳转商品详情页时,因为详情页有很多ajax请求,并且都用的同步请求,就会导致请求时浏 ...

  2. Android UI相关开源项目库汇总

    最近做了一个Android UI相关开源项目库汇总,里面集合了OpenDigg 上的优质的Android开源项目库,方便移动开发人员便捷的找到自己需要的项目工具等,感兴趣的可以到GitHub上给个st ...

  3. HLOCAL 初探

    首先看一段程序,输出的结果为两个相同的整数(这两个整数是内存地址). #include "stdafx.h" #include <windows.h> int _tma ...

  4. 自动生成编号(B开头后跟6位,数据库查询不重复)

    private string GetAccountNo() { try { string shortName="B"; "; //查询数据库 7位且包含“B” & ...

  5. Spring、Springmvc整合web的web.xml配置

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...

  6. php静态变量与方法与phar的使用

    本节用类与静态变量改造之前的例子:php根据命令行参数生成配置文件 ghostinit.php: <?php class ghostinit{ static $version = 'ghost ...

  7. mysql-学习笔记1

    1.while循环的一个方便的用法是循环处理一个SQL查询结果中的数据行. 2.mysqli_fetch_array(),这个内置的PHP函数从一个数据库查询的结果中获取一个数据行,可以搭配while ...

  8. 【代码笔记】iOS-json文件的使用

    一,工程图. 二,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the ...

  9. 【代码笔记】iOS-键盘自适应弹出

    一,效果图. 二,工程图. 三,代码. ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIVie ...

  10. <Android 基础(三十一)> ObjectAnimator

    简介 ObjectAnimator,是ValueAnimator的子类,支持利用目标视图的属性来实现动画效果.构造ObjectAnimator的时候,将会提取对应的参数来定义动画对象和对象属性.合适的 ...