c++ 遍歷查找win指定路徑下指定類型的文件

#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include <io.h>
#include <windows.h>  
#include <string>
#include <vector>
#include <iomanip>

#define MY_NOT_CHECK  1;
#define MY_CHECKED  0;
using namespace std;

int main(){
    string path("c:\\Windows\\system32"), exd("exe");
    vector<WIN32_FIND_DATA> files;
    void getFiles(string path, string exd, vector<WIN32_FIND_DATA>& files);
    
    getFiles(path, exd, files);
   
    sort(files);

    show(files);
}

/*遍歷文件夾,獲得全部exe文件*/
void getFiles(string path, string exd, vector<WIN32_FIND_DATA>& files)
{
    /************************************************************************/
    /*  獲取文件夾下全部文件名
    輸入:
    path    :    文件夾路徑
    exd        :   所要獲取的文件名後綴,如jpg、png等;
    文件名, exd = ""
    輸出:
    files    :    獲取的文件名列表
    */
    /************************************************************************/
    BOOL SetPrivilege(HANDLE hToken, LPCTSTR lpszPrivilege, BOOL bEnablePrivilege);

    //文件句柄
    HANDLE  hFile = INVALID_HANDLE_VALUE,hExe = INVALID_HANDLE_VALUE;
    //文件信息
    WIN32_FIND_DATA fileinfo,exeinfo;
    string pathName, exdName = "\\*";
    PVOID OldValue = NULL;
    
    // 關閉系統重定向
    if (Wow64DisableWow64FsRedirection(&OldValue))
    {
        // 查找指定路徑
        hFile = FindFirstFile(pathName.assign(path).append(exdName).c_str(), &fileinfo);

        if (FALSE == Wow64RevertWow64FsRedirection(OldValue))
        {
            return;
        }
    }
    

    // 查找指定路徑
    // hFile = FindFirstFile(pathName.assign(path).append(exdName).c_str(), &fileinfo);

    // 是否查找失敗
    if (hFile == INVALID_HANDLE_VALUE)
    {
        // 是否由於權限不足,若不足,則提高權限
        if (GetLastError() == 5)
        {
            HANDLE hToken;
            BOOL bRet = OpenProcessToken(
                GetCurrentProcess(),    // 進程句柄(當前進程)
                TOKEN_ALL_ACCESS,    // 全權訪問令牌
                &hToken    // 返回的參數 進程令牌句柄 (就是AdjustTokenPrivileges的第一個參數)
                ); // 獲取進程的令牌句柄
            if (bRet != TRUE)
            {
                cout << "獲取令牌句柄失敗!" << endl;
                return;
            }
            BOOL set = SetPrivilege(hToken, SE_DEBUG_NAME, TRUE);
            if (!set || GetLastError() != ERROR_SUCCESS) {
                // 設置權限失敗
                cout << "提高權限失敗 error:" << GetLastError() << endl;
                cout << "此文件夾缺乏權限訪問:    " << pathName.assign(path).append("\\").c_str() << endl;
                return;
            }
            // 權限設置成功,繼續執行
            hFile = FindFirstFile(pathName.assign(path).append(exdName).c_str(), &fileinfo);
            cout << "權限設置完成" << endl;
            cout << GetLastError()<<endl;
        }
        else
        {
            // 不是權限問題
            cout << "FindFirstFile failed " << GetLastError() << endl;
            system("pause");
            return;
        }
    }

    int flag = MY_NOT_CHECK;
    int lastError = 0;

    // 遍歷
    do
    {
        //若是是文件夾,迭代之
        if ((fileinfo.dwFileAttributes &  FILE_ATTRIBUTE_DIRECTORY))
        {
            if (strcmp(fileinfo.cFileName, ".") != 0 && strcmp(fileinfo.cFileName, "..") != 0)
                getFiles(pathName.assign(path).append("\\").append(fileinfo.cFileName), exd, files);
        }
        else
        {
            //若是不是文件夾
            /*
            if (strcmp(fileinfo.cFileName, ".") != 0 && strcmp(fileinfo.cFileName, "..") != 0)
            {
                for (int i = 0; fileinfo.cFileName[i + 3] != '\0'; i++)
                {
                    // 判斷是否exe
                    if (fileinfo.cFileName[i] == '.'
                        && (fileinfo.cFileName[i + 1] == 'e' || fileinfo.cFileName[i + 1] == 'E')
                        && (fileinfo.cFileName[i + 2] == 'x' || fileinfo.cFileName[i + 2] == 'X')
                        && (fileinfo.cFileName[i + 1] == 'e' || fileinfo.cFileName[i + 1] == 'E')
                        && fileinfo.cFileName[i + 4] == '\0')
                    {
                        files.push_back(fileinfo);
                        break;
                    }
                }
            }
            */
            // 若是當前目錄還未查找過,查找當前目錄的exe文件
            if (flag) 
            {
                // 關閉系統重定向
                if (Wow64DisableWow64FsRedirection(&OldValue))
                {
                    // 查找指定路徑
                    hExe = FindFirstFile(pathName.assign(path).append("\\*." + exd).c_str(), &exeinfo);

                    if (FALSE == Wow64RevertWow64FsRedirection(OldValue))
                    {
                        return;
                    }
                }
                // hExe = FindFirstFile(pathName.assign(path).append("\\*.exe").c_str(), &exeinfo);
                if (hExe == INVALID_HANDLE_VALUE)
                {
                    lastError = GetLastError();
                    if (lastError == 2)
                    {
                        //cout << setiosflags(ios::left) << setw(50) << path << " 此目錄下沒有exe " << endl;
                    }
                    else
                    {
                        cout << " 查找exe失敗 " << lastError << endl;
                        return;
                    }
                }
                // 遍歷全部本文件夾下的exe文件
                if (lastError != 2)
                {
                    do
                    {
                        files.push_back(exeinfo);
                    } while (FindNextFile(hExe, &exeinfo));
                    // 查找完成,此目錄已不用遍歷,跳出
                    flag = MY_CHECKED;
                    FindClose(hExe);
                }
            }
        }
    } while (FindNextFile(hFile, &fileinfo));
    FindClose(hFile);
    return;
}
相關文章
相關標籤/搜索