Windows下文件检索的基本姿势
要点
使用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("\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下文件检索的基本姿势的更多相关文章
- 转:windows下命令行工具
转自: http://www.cnblogs.com/haochuang/p/5593411.html Windows下CMD不好用,远没有Linux,或者一些SSH工具用起来方便.其实Windows ...
- windows下使用体验更好的控制台——ConsoleZ
转做前端开发以来,每天使用最频繁的工具就是控制台了,git提交代码要用,npm安装node包也要用,grunt task 也要用,可是系统自带的cmd太难用了, 那么问题就来了: "wind ...
- [爬虫]Windows下如何安装python第三方库lxml
lxml是个非常有用的python库,它可以灵活高效地解析xml与BeautifulSoup.requests结合,是编写爬虫的标准姿势. 但是,当lxml遇上Windows,简直是个巨坑.掉在安装陷 ...
- [转]Linux/Windows下脚本对拍程序
[新]简单写法 (转载自:https://blog.csdn.net/ylsoi/article/details/79824655) 要求:文件输入输出,且输入输出文件需要对应 Linux: #inc ...
- windows下sqli-labs的搭建及学习(GET篇)
环境搭建: 源码下载地址:https://github.com/Audi-1/sqli-labs 需要搭建以下环境: apache+mysql+php Tomcat+mysql+java(部分关卡需要 ...
- Windows下如何安装python第三方库lxml
lxml是个非常有用的python库,它可以灵活高效地解析xml,与BeautifulSoup.requests结合,是编写爬虫的标准姿势. 参考 Windows下如何安装python第三方库lxml ...
- 在windows下安装gulp —— 基于 Gulp 的前端集成解决方案(一)
相关连接导航 在windows下安装gulp —— 基于 Gulp 的前端集成解决方案(一) 执行 $Gulp 时发生了什么 —— 基于 Gulp 的前端集成解决方案(二) 常用 Gulp 插件汇总 ...
- 让 windows 下的命令行程序 cmd.exe 用起来更顺手
在 Windows 下使用 Larave 框架做开发,从 Composer 到 artisan 总是避免不了和 cmd.exe 打交道,系统默认的命令行界面却是不怎么好看,且每行显示的字符数是做了限制 ...
- Windows下Visual studio 2013 编译 Audacity
编译的Audacity版本为2.1.2,由于实在windows下编译,其源代码可以从Github上取得 git clone https://github.com/audacity/audacity. ...
随机推荐
- Java NIO系列教程(八) SocketChannel
Java NIO中的SocketChannel是一个连接到TCP网络套接字的通道.可以通过以下2种方式创建SocketChannel: 打开一个SocketChannel并连接到互联网上的某台服务器. ...
- MySQL、Mariadb 复制原理
复制的作用 l 水平扩展 l 数据备份 l 数据分析 l 数据分布 l 高可用性 复制的工作原理 Mariadb的复制功能是基于binlog进行的.复制的工作主要是由主库上Master du ...
- maven3.6.1-02初体验及jar包上传
安装当前最新版本的nexus,安装教程网上搜,不多说了. 因为nexus3x版本没有2x版本中内置的3rd_part,所以不能在界面中上传jar包,必须使用maven的命令行. 添加第三方仓库,名字 ...
- .7-浅析express源码之Router模块(3)-app[METHODS]
之前的讨论都局限于use方法,所有方式的请求会被通过,这一节讨论express内部如何处理特殊请求方法. 给个流程图咯~ 分别给出app.METHODS与router.METHODS: // app. ...
- NPOI 之导入导出
转自https://www.cnblogs.com/zuowj/archive/2015/05/04/4475663.html转别人的,做了一点点改动 using NPOI.HSSF.UserMode ...
- Oracle XE快捷版(速成版)的限制
1.CPU上限:无论把数据库安装在多少核的服务器上,都只会提供一个CPU核心的运算能力 2.安装和执行限制:只能安装一个实例且只能运行一个实例 3.用户数据上限:最大11G的用户数据 4.内存使用上限 ...
- JUC源码1-原子量
什么是原子量,原子量就是一次操作,要么成功,要么失败.比如java中的i++ 或i-- , 不具备原子性,每次读取的值都是不一样的,探究其原因,x86体系中,他的总线是32位的,i++的操作指令必须是 ...
- [android] 新闻客户端引入SlidingMenu
下载SlidingMenu,https://github.com/jfeinstein10/SlidingMenu 导入library 我们项目右键==>Properties==>Andr ...
- Mybatis标签bind用法
Mybatis使用bind元素进行模糊查询,不用在乎数据库是mysql还是oracle从而提高可移植性 使用bind元素传递多个参数 public List<Student> findSt ...
- Vue:模板&渲染函数学习
模板&渲染函数区别: 1.代码量:模板代码重复逐行拼写,渲染函数可以迭代拼接方式实现重复代码. 2.函数式组件中应用:基于模板的函数式组件需要手动添加特性和事件,给予渲染函数的函数是组件使用c ...