美文网首页
Windows下文件检索的基本姿势

Windows下文件检索的基本姿势

作者: PeterZ1997 | 来源:发表于2018-08-13 00:17 被阅读0次

要点

使用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下文件检索的基本姿势

    要点 使用FindFirstFile和FindNextFile两个WindowsAPI,并配合链表或队列存储文件夹...

  • Linux日记本_02:文件的基本操作

    文件的基本操作 1.删除文件 在Windows下若要将某个文件删除,我们使用的是del命令,而在Linux下可以使...

  • 【Linux】3. Linux文件目录介绍

    Windows 和 Linux 文件系统区别 -- 结构 Windows 下的文件系统 - 在 Windows 下...

  • Mac终端命令

    OSX 采用的Unix文件系统,所有文件都挂在跟目录 / 下面,所以不在要有Windows 下的盘符概念。 基本命...

  • dos2unix替换方案

    在linux下,去除^M,将windows格式文件(dos文件)改为unix格式文件 在Windows系统下编辑的...

  • Linux 命令行复习

    在公司基本使用的是Windows系统,导致Linux系统的命令不是很熟悉; 记录一下Linux基本的文件操作!其实...

  • JAVA并发编程-基本概念

    基本概念 程序、进程、线程之间的概念 程序是静态的概念windows下通常指exe 文件 linux 下 ja...

  • hosts文件的作用

    Window系统中有个Hosts文件,Windows98系统下该文件在Windows目录,在Windows2000...

  • 文件格式汇总

    ★Windows环境下的文件后缀名 绝大多数DOS文件名后缀在Windows下继续有效,但Windows本身也引出...

  • windows 下文件扩展名 含义

    ★Windows环境下的文件后缀名绝大多数DOS文件名后缀在Windows下继续有效,但Windows本身也引出了...

网友评论

      本文标题:Windows下文件检索的基本姿势

      本文链接:https://www.haomeiwen.com/subject/qgtebftx.html