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
}