C + + non-recursive traversal of disk files and recursive traversal of disk files example _c language

Source: Internet
Author: User
Tags locale

1: Non-recursive method:

Copy Code code as follows:

File NAME:CSEARCH.H

#pragma once
#include <vector>
#include <atlstr.h>
#include <stack>

Class Search
{
Private
Std::vector<cstring> M_strpath; Save Lookup to file path
Std::vector<cstring> M_strsearchname; Keywords for search
Std::stack<cstring> Strpathstack; Stacks, saving the disk ID

void Listallfileindrectory (CString strpath);


Public
Search ();
~search ();

void Start (void); Start Search
};

Copy Code code as follows:

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 settings for a language area
Free (old_locale);//Restore region settings

File. Close ();
}

TCHAR Strbuffer[50] = {0};
TCHAR * pstr = Strbuffer;
CString Strtempname;

Getting disk drives
GetLogicalDriveStrings (M, strbuffer);

Strtempname = Strbuffer;
while (Strtempname!= _t (""))
{
If the 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
        {
           //filtration "." 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 is not a System attribute catalog press 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 cause a memory overflow
}

2: Recursive method

Copy Code code as follows:

File NAME:CSEARCH.H

#pragma once
#include <vector>
#include <atlstr.h>
#include <stack>

Class Search
{
Private
Std::vector<cstring> M_strpath; Save Lookup to file path
Std::vector<cstring> M_strsearchname; Keywords for search

void Listallfileindrectory (CString strpath);


Public
Search ();
~search ();

void Start (void); Start Search
};

Copy Code code as follows:

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 settings for a language area
Free (old_locale);//Restore region settings

File. Close ();
}

TCHAR Strbuffer[50] = {0};
TCHAR * pstr = Strbuffer;
CString Strtempname;

Getting disk drives
GetLogicalDriveStrings (M, strbuffer);

Strtempname = Strbuffer;
while (Strtempname!= _t (""))
{
If the 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
        {
           //filtration "." 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 is not a system property directory recursive call
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 cause a memory overflow
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.