要点

使用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. 关于SAN和NAS的区别-转

    什么是SAN与NAS By  王文平 发表于 2006-7-10 18:03:53  NAS和SAN字面上相似,并且都是新型数据存储模式,但这二者是完全不同的,针对不同方向的技术. 什么是SAN(St ...

  2. (转)JAVA常见面试题之Forward和Redirect的区别

    阅读目录 一:间接请求转发(Redirect) 二:直接请求转发(Forward) 用户向服务器发送了一次HTTP请求,该请求可能会经过多个信息资源处理以后才返回给用户,各个信息资源使用请求转发机制相 ...

  3. 【SpringBoot系列4】SpringBoot定制自己的bean

    起因:SpringBoot我是越用越喜欢,但当SpringBoot出了问题的时候,我却无从下手,因为封装实在是太高度化了.然后之前有一个需求,使用SpringBoot提供的StringRedisTem ...

  4. Excel核心技巧【干货】

    进入职场后发现,几乎有很大一部分时间都耗在了表格上. Excel的存在是为了更高效工作,但庞大的数据处理却成了你每晚加班的“凶手”? 其实,从数据整理到数据分析,只要掌握20%的Excel技巧,就足以 ...

  5. [javaSE] 看博客学习java并发编程

    共享性 多线程操作同一个数据,产生线程安全问题 新建一个类ShareData 设计一个int 型的成员变量count 设计一个成员方法addCount(),把count变量++ 在main函数中开启多 ...

  6. Codeforces841B

    B. Godsend time limit per test:2 seconds memory limit per test:256 megabytes input:standard input ou ...

  7. Codeforces35E(扫描线)

    E. Parade time limit per test:2 seconds memory limit per test:64 megabytes input:input.txt output:ou ...

  8. JavaScript的三种对话框是通过调用window对象的三个方法alert(),confirm()和prompt()

    第一种:alert()方法 alert()方法是这三种对话框中最容易使用的一种,她可以用来简单而明了地将alert()括号内的文本信息显示在对话框中,我们将它称为警示对话框,要显示的信息放置在括号内, ...

  9. JavaSE——TCP协议网络编程(二)

    1.Java网络编程与多线程的综合应用: 类Socket提供了方法getInputStream ()和getOutStream()来得到对应的输入/输出流以进行读/写操作,这两个方法分别返回Input ...

  10. ES6 箭头函数下的this指向

    在javscript中,this 是在函数运行时自动生成的一个内部指针,它指向函数的调用者. 箭头函数有些不同,它的this是继承而来, 默认指向在定义它时所处的对象(宿主对象),而不是执行时的对象. ...