// Class name CBROWSEDIR / / / BROWSEDIR.H file #include "stdlib.h"
Class CBROWSEDIR {protected: / / Store the absolute path of the initial directory, with '/' end char m_szinitdir [_max_path];
Public: // Default constructor CBROWSEDIR ();
/ / Set the initial directory as DIR, if false is returned, indicating that the directory is not available for Bool SetinitDir;
// Start traversing the initial directory and its subdirectory file // filespec can use wildcard *?, The path is not included. / / If false returns, it means that the traversal process is aborted by the user to Bool BeginBrowse (Const Char * filespec);
Protected: // Trade the directory DIR by filespec's file // For subdirectories, use iteration methods // If false, indicate abort traversal file Bool Browsedir (Const Char * Dir, Const Char * filespec);
// Function Browsedir finds a file, call processFile // and pass the file name as a parameter to pass // If false, indicates that the abort trail file // user can override the function, join your own processing code Virtual Bool ProcessFile ( Const char * filename);
// Each time, each of the entered a directory, call processdir // and pass the directory name and the last level directory name as a parameter to pass // if it is processing, the parentdir = NULL // user can overclock Write this function, add your own processing code //, such as the user can count the number of Virtual Void ProcessDir (const char * currentdir, const char * parentdir);}; /// browsedir.cpp file / # include " STDAFX.H "#include" stdlib.h "#include" direct.h "#include" string.h "#include" io.h "
#include "browsedir.h"
#ifdef _debug # define new debug_new # undef this_filestatic char this_file [] = __file __; # ENDIF
CBROWSEDIR :: CBROWSEDIR () {// initialized M_SZinitDirgetCwd (M_Szinitdir, _max_path) with the current directory;
// If the last letter of the directory is not '/', then add one '/' int Len = Strlen (m_szinitdir); if (m_szinitdir [len-1]! = '//') strcat (m_szinitdir, " // ");
Bool CBROWSEDIR :: setinitdir (const char * dir) {// First convert DIR to absolute path if (_FullPath (m_szinitdir, dir, _max_path) == null) Return False;
/ / Judgment whether the directory exists if (_chdir (m_szinitdir)! = 0) Return False;
// If the last letter of the directory is not '/', then add one '/' int Len = Strlen (m_szinitdir); if (m_szinitdir [len-1]! = '//') strcat (m_szinitdir, " // ");
Return True;}
bool CBrowseDir :: BeginBrowse (const char * filespec) {ProcessDir (m_szInitDir, NULL); return BrowseDir (m_szInitDir, filespec);} bool CBrowseDir :: BrowseDir (const char * dir, const char * filespec) {_ chdir (dir);
// First find the document line hfile; _finddata_t fileinfo; if ((HFILE = _FindFirst (Filespec, & fileInfo))! = -1) {DO {// If not, proceed if {char filename [_MAX_PATH]; strcpy (filename, dir); strcat (filename, fileinfo.name); if return false (ProcessFile (filename)!);} ((fileinfo.attrib & _A_SUBDIR)!)} while (_findnext (HFILE, & FILEINFO) == 0); _FindClose (HFile);
/ / Find subdirectories in DIR // Because when processed files in Dir, the processfile of the derived class may change the // current directory, so reset the current directory as DIR. After / / After performing _findfirst, the relevant information may be recorded, so changing the directory // has no impact on _findNext. _chdir (DIR); if ((HFile = _findfirst ("*. *", & fileInfo)! = -1) {DO {// Check is the directory // If yes, check is. or .. // If not, iterate IF (FileInfo.ttrib & _a_Subdir) {IF (fileinfo.name, ".")! = 0 && strcmp (fileinfo.name, ".")! = 0) {char Subdir [_MAX_PATH]; STRCPY (SUBDIR, DIR); STRCAT (SUBDIR, FILEINFO.NAME); STRCAT (SUBDIR, "//"); ProcessDir (Subdir, Dir); if (! Browsedir (Subdir, FileSpec) Return False; }}} While (_findNext (hfindnext (hfindnext (fileinfo) == 0); _ findclose (hfile);} return true;
Bool CBROWSEDIR :: ProcessFile (const char * filename) {return true;}
Void CBROWSEDIR :: ProcessDir (const char * currentdir) {}