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. ...
随机推荐
- android studio 断网使用
- 获取ip 笔记
使用命令 Request.UserHostAddress;//IP地址 Request.UserHostName;//用户名. 1. 在ASP.NET中专用属性: 获取服务器电脑名:Page.Serv ...
- js 中格式化时间
在js中常常要求对时间的输出格式进行格式化,比如 2017-01-01 10:10,比较麻烦的是月,日,小时,分.它们的格式一般要求两位,如果小于10的话需要在前边补0,当然这算不上什么问题,可以通过 ...
- 处理html内容,获取纯文本
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import java.util.rege ...
- 客服端与服务端APP支付宝支付接口联调的那些坑
根据支付宝官方提供的文档的建议: TIPS:这一步应在商户服务端完成,商户服务端直接将组装和签名后的请求串orderString传给客户端,客户端直接传给SDK发起请求.文档和Demo是为了示例效果在 ...
- C#常见几道面试题
首先碰到的是这样的一首题目:计算数组{1,1,2,3,5,8.......} 第30位值,不用递归,我写出了以下这样的代码: static void Main(string[] args) { ]; ...
- (第一篇) 一步一步带你了解linq to Object
要想学好linq to object 我们必须要先学习lambda 表达式,学习lambda 表达式呢我们必须了解匿名函数和匿名类及扩展方法,学习匿名函数,我们必须学会委托,这是本文的宗旨.下面开始第 ...
- [android] 轮播图-滑动图片标题焦点
谷歌提供的v4包,ViewPager 在布局文件中,先添加<android.support.v4.view.ViewPager/>控件,这个只是轮播的区域 在布局文件中,布置标题描述部分 ...
- git 提交规范
git 提交规范 前言 无规矩不成方圆,编程也一样. 如果你有一个项目,从始至终都是自己写,那么你想怎么写都可以,没有人可以干预你.可是如果在团队协作中,大家都张扬个性,那么代码将会是一团糟,好好的项 ...
- spring boot aop 报错
1.控制台报错 Pointcut is not well-formed: expecting 'name pattern' at character position 33 execution(com ...