// Find_file.cpp: defines the entry point for the console application.//
#include "stdafx.h"
Test required head file #include
// x_find_file class required header file #include
Using namespace std;
// Features: Find files in the specified directory and its subdirectories, encapsulate the Win32 API. Search result creation time of the order by file: Chen Lifu (AARON)
Class x_find_file {private: win32_find_data find_data; handle find_handle;
Bool Find_Sub; Whether to find the subdirectory BOOL FIND_EXTENT; whether to find a preference BOOL DIR_CHANGED; whether it changed the current lookup directory Char Separator; file directory separator, the Windows file system is '//' string origin_dir; the original directory string current_dir; current lookup Contents String Find_FileName; Find file, can contain wildcard string find_result; the result of the last lookup, does not include path list
PUBLIC:
X_Find_File () {FIND_SUB = FALSE; default is not searching for subdirectory FIND_EXTENT = true; default Search Priority DIR_CHANGED = FALSE; Separator = '//'; file directory separator, Windows file system is '//' find_handle = invalid_handle_value;} ~ x_find_file () {if (find_handle = INVALID_HANDLE_VALUE!) {:: FindClose (find_handle); find_handle = INVALID_HANDLE_VALUE;}} private: not replicated object x_find_file (const x_find_file &); operator = (const x_find_file &);
PUBLIC: Features: Read the subdirectory in the specified directory, do not continue the search input in its subdirectory: [DIR]: Directory to perform [Separator]: directory separator, default is the '//' output under Windows : [SUB_SIRS]: Find the subdirectory list, press the list of sequences to add the list list The original data will not be changed, if the lookup failed (no subdirectory), the list will not be changed to each sub-directory item contains [ DIR] Return Value: Find the subdirectory Returns true, otherwise returning false This function is declared as static static bool pick_sub_dirs (const string & dir, list
Go to the next directory to find the first file and change the current lookup directory as long as the next directory can continue to find the lookup until the first file is found and returns true, otherwise return false means no more directory Continue to find BOOL FIND_IN_NEXT_DIR () {while (Find_Dirs.size ()> 0) {Change the current lookup directory for the first directory to look for the list of directory and remove the directory out to the directory list current_dir = find_dirs.front (); find_dirs .POP_FRONT (); DIR_CHANGED = true; if (Find_First_File ()) {return true;}} Return false;} public: Set the original directory and the first lookup directory void set_origin_dir (const string & dir) {Origin_Dir = DIR; Current_dir = dir;}
Read the original directory name string get_origin_dir () const {return origin_dir;} Set and read the directory separator of the directory separator void set_separator (char ch) {Windows file system is '//' Separe = Ch;} Return to the class inside Directory separator is to use the same directory separator in the class without explicit use of the same directory separator instead of the same directory separator instead of the same directory separator instead of using the file name. () const {return separator;}
Set the file name to be found, you can include a wildcard, or include the path name if the path name is included, then the path is the path from the set_origin_dir function set. Void set_fink_filename (const string & filename) {FIND_FILENAME = filename;}
Read the file name String get_filename () const {return_filename ()
Set and read whether the subdirectory void set_find_sub (bool find) {FIND_SUB = Find;} BOOL IS_FIND_SUB () Const {Return Find_Sub;}
Setting and reading whether the precedence Void set_find_extent (bool eXtent) {FIND_EXTENT = Extent;} BOOL IS_FIND_EXTENT () Const {Return Find_extent;}
Detect whether to switch the current lookup directory BOOL IS_DIR_CHANGED () const {return dir_changed;}
Read the file name of the last lookup, does not include the path string get_filename () const {return, read the last lookup file name contains the path started from the path set from the set_origin_dir function String get_path_filename () const {char filename [ 2 * max_path]; strcpy (filename, current_dir.c_str ()); int Dir_len = Strlen (filename); if (Dir_len> 0) {IF (filename [Dir_len - 1]! = Separator) {filename [Dir_len] = Separator FILENAME [DIR_LEN 1] = '/ 0';}} strcat (filename, find_result.c_str ()); return filename;} Read the current execution lookup The path started from the path set from the set_origin_dir function String get_current_dir () const {return current_dir;}
Finding the first file in the current directory If you find the first file, return true or if there is more directory you can continue to find (lookup subdirectory), continue to find the first file or in the next directory. Without more directorys, you can continue to find the first file. If you return true, you return false to perform a lookup, the function should first be called and only call one otherwise not the correct lookup result bool Find_First_File () {IF (current_dir.size () <1) {returnaf false;}
IF (Find_FileName.Size () <1) {return false;} if (find_sub) {If you perform a lookup in a subdirectory, the subdirectory of the current directory is read and the insertion of these subdirectories should be inserted according to the breadth or depth. Go to the tail or header of the list of directories to be found, the order in the list is the order they have been looking for in the current directory to find the first file, its subdirectory is read out.
list
// char directory [2 * MAX_PATH]; strcpy (directory, current_dir.c_str ()); int dir_len = strlen (directory); if (! Directory [dir_len - 1] = separator) {directory [dir_len] = separator; directory [DIR_LEN 1] = '/ 0';} char far find_string [2 * max_path]; strcpy (find_string, directory); strcat (find_string, find_filename.c_str ()); this situation should not appear if (Find_Handle! = INVALID_HANDLE_VALUE) {:: FindClose (find_handle); find_handle = INVALID_HANDLE_VALUE;} find_handle = :: FindFirstFile (find_string, & find_data);! if (find_handle = INVALID_HANDLE_VALUE) {find_result = find_data.cFileName; return true;} in the current directory lookup fails, Continue to find an indirect handset here for the next directory Find_First_File return find_in_next_dir ();}
Find the next file in the current directory If you return true if you have more directorys, you can continue to find (find the subdirectory), continue in the next directory, you can find the first file or there is no more directory. Continue to find this if you find the first file, returning to false bool find_next_file () {DIR_CHANGED = FALSE;
IF (Find_Handle! = INVALID_HANDLE_VALUE) {if (:: findnextfile (find_handle, & find_data) {find_result = find_data.cfilename;
Return True;}
:: Findclose (Find_Handle); Find_Handle = INVALID_HANDLE_VALUE;}
When the current directory looks, then find returnof_in_next_dir ();}};
//
INT Main (int Argc, char * argv []) {test x_find_file finder; can not copy object x_find_file f1 (finder); x_find_file f2 = finder; x_find_file f3; f3 = finder;
Finder.set_forigin_dir ("c: //"); Finder.set_find_filename ("*. *"); Finder.Set_Find_Sub (true); Finder.Set_Find_extent (false); if (Finder.Find_FIRST_FILE ()) Find_First_File is called and only Call once {cout << "/ ncurrent_directory: / n" << Finder.get_current_dir () << Endl; do {if (Finder.is_dir_changed ()) {cout << "/ npress 'q' to quit, else to Continue ... "<< Endl; IF (getch () == 'q') {breaf;} cout <<" / ncurrent_directory: / n "<< Finder.get_current_dir () << endl;}
Cout << Finder.get_FileName () << endl; cout << finder.get_path_filename () << endl;}
While (Finder.find_Next_File ());
Return 0;}