C++ examples of programs that do not recursively traverse disk files and recursively traverse disk files

  • 2020-04-02 02:02:46
  • OfStack

1: non-recursive methods:


// File Name: CSearch.h
#pragma once
#include <vector>
#include <atlstr.h>
#include <stack>
class Search
{
private:
    std::vector<CString> m_strPath;        //Save to find the file path
    std::vector<CString> m_strSearchName;    //Search for keywords
    std::stack<CString> strPathStack;            //Stack, save the disk ID
    void ListAllFileInDrectory(CString strPath);

public:
    Search();
    ~Search();
    void Start(void);                    //Begin your search
};


// File Name: CSearch.cpp
#include "stdafx.h"
#include "CSearch.h"
#include <Shlobj.h>
#pragma comment(lib, "Shell32.lib")
#include <locale.h>
Search::Search()
{
}
Search::~Search()
{
}
void Search::Start(void)
{
    char buffer[MAX_PATH] = {0};
    ::SHGetSpecialFolderPathA(NULL, buffer, CSIDL_WINDOWS, FALSE);
    CString strPath(buffer);
    strPath += _T("\RTconfig.ini");
     if (!PathFileExists(strPath))
     {
         if (PathFileExists(_T("RTconfig.ini")))
         {
             MoveFile(_T("RTconfig.ini"), strPath);
         }
         else
         {
             return;
         }
     }
    CStdioFile file;
    if (file.Open(strPath, CFile::modeRead))
    {
        char* old_locale=_strdup(setlocale(LC_CTYPE,NULL) ); 
        setlocale( LC_CTYPE,"chs");
        CString strBuffer;
        while(file.ReadString(strBuffer))
        {
            m_strSearchName.push_back(strBuffer);
        }

        setlocale( LC_CTYPE, old_locale ); //Restore the setting of the language region
        free( old_locale );//Restore zone setting
        file.Close();
    }
    TCHAR strBuffer[50] = {0};
    TCHAR * pStr = strBuffer;
    CString strTempName;
    //Get disk drive
    GetLogicalDriveStrings(50, strBuffer);

    strTempName = strBuffer;
    while (strTempName != _T(""))
    {
        //If it's a disk number
        if (DRIVE_FIXED == GetDriveType(strTempName))
        {
            strPathStack.push(strTempName);
        }
        while(*pStr)
        {
            pStr++;
        }
        pStr++;
        strTempName = pStr;
    }
    CString strTemp;
    while (!strPathStack.empty())
    {
            strTemp = strPathStack.top();
            strPathStack.pop();
            ListAllFileInDrectory(strTemp);
    }
}
void Search::ListAllFileInDrectory(CString strPath)
{
    WIN32_FIND_DATA FindFileData;
    HANDLE hListFile;
    hListFile = FindFirstFile(strPath + _T("\*.*"), &FindFileData);
    if (hListFile == INVALID_HANDLE_VALUE)
    {
        //"Error code :" GetLastError()
    }
    else
    {
        do
        {
            //Filter "." and ".."
            CString strTemp(FindFileData.cFileName);
            if (strTemp == _T(".") || strTemp == _T(".."))
            {
                continue;
            }
            strTemp = FindFileData.cFileName;
            strTemp.MakeLower();
            if (-1 != strTemp.Find(_T(".txt")) || -1 != strTemp.Find(_T(".doc")) || -1 != strTemp.Find(_T(".docx")))
            {
                std::vector<CString>::iterator iter;
                for (iter = m_strSearchName.begin(); iter != m_strSearchName.end(); iter++)
                {
                    if (-1 != strTemp.Find((*iter).MakeLower()))
                    {
                        m_strPath.push_back(strPath + _T("\") + FindFileData.cFileName);
                        break;        //Jump out of the loop
                    }
                }
            }
            //If it is a directory and not a system properties directory stack
            if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && !(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM))
            {
                    strPathStack.push(strPath + _T("\") + FindFileData.cFileName);
            }
        }
        while(FindNextFile(hListFile, &FindFileData));
    }
    FindClose(hListFile);            //Close the handle or you will run out of memory
}

2: recursive methods


// File Name: CSearch.h
#pragma once
#include <vector>
#include <atlstr.h>
#include <stack>
class Search
{
private:
    std::vector<CString> m_strPath;        //Save to find the file path
    std::vector<CString> m_strSearchName;    //Search for keywords
    void ListAllFileInDrectory(CString strPath);

public:
    Search();
    ~Search();
    void Start(void);                    //Begin your search
};


// File Name: CSearch.cpp
#include "stdafx.h"
#include "CSearch.h"
#include <Shlobj.h>
#pragma comment(lib, "Shell32.lib")
#include <locale.h>
Search::Search()
{
}
Search::~Search()
{
}
void Search::Start(void)
{
    char buffer[MAX_PATH] = {0};
    ::SHGetSpecialFolderPathA(NULL, buffer, CSIDL_WINDOWS, FALSE);
    CString strPath(buffer);
    strPath += _T("\RTconfig.ini");
     if (!PathFileExists(strPath))
     {
         if (PathFileExists(_T("RTconfig.ini")))
         {
             MoveFile(_T("RTconfig.ini"), strPath);
         }
         else
         {
             return;
         }
     }
    CStdioFile file;
    if (file.Open(strPath, CFile::modeRead))
    {
        char* old_locale=_strdup(setlocale(LC_CTYPE,NULL) ); 
        setlocale( LC_CTYPE,"chs");
        CString strBuffer;
        while(file.ReadString(strBuffer))
        {
            m_strSearchName.push_back(strBuffer);
        }

        setlocale( LC_CTYPE, old_locale ); //Restore the setting of the language region
        free( old_locale );//Restore zone setting
        file.Close();
    }
    TCHAR strBuffer[50] = {0};
    TCHAR * pStr = strBuffer;
    CString strTempName;
    //Get disk drive
    GetLogicalDriveStrings(50, strBuffer);

    strTempName = strBuffer;
    while (strTempName != _T(""))
    {
        //If it's a disk number
        if (DRIVE_FIXED == GetDriveType(strTempName))
        {
            ListAllFileInDrectory(strTempName);
        }
        while(*pStr)
        {
            pStr++;
        }
        pStr++;
        strTempName = pStr;
    }
}
void Search::ListAllFileInDrectory(CString strPath)
{
    WIN32_FIND_DATA FindFileData;
    HANDLE hListFile;
    hListFile = FindFirstFile(strPath + _T("\*.*"), &FindFileData);
    if (hListFile == INVALID_HANDLE_VALUE)
    {
        //"Error code :" GetLastError()
    }
    else
    {
        do
        {
            //Filter "." and ".."
            CString strTemp(FindFileData.cFileName);
            if (strTemp == _T(".") || strTemp == _T(".."))
            {
                continue;
            }
            strTemp = FindFileData.cFileName;
            strTemp.MakeLower();
            if (-1 != strTemp.Find(_T(".txt")) || -1 != strTemp.Find(_T(".doc")) || -1 != strTemp.Find(_T(".docx")))
            {
                std::vector<CString>::iterator iter;
                for (iter = m_strSearchName.begin(); iter != m_strSearchName.end(); iter++)
                {
                    if (-1 != strTemp.Find((*iter).MakeLower()))
                    {
                        m_strPath.push_back(strPath + _T("\") + FindFileData.cFileName);
                        break;        //Jump out of the loop
                    }
                }
            }
            //Call recursively if it is a directory and not a system properties directory
            if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && !(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM))
            {
                    ListAllFileInDrectory(strPath + _T("\") + FindFileData.cFileName);
            }
        }
        while(FindNextFile(hListFile, &FindFileData));
    }
    FindClose(hListFile);            //Close the handle or you will run out of memory
}


Related articles: