Traverse the entire directory tree find file

xiaoxiao2021-03-06  15

In the specific statement below, you can see that the function search_directory will be traveled to find the function Search_Directory after the name of the file name to be found and the name of the directory to be searched. First look for each entity (file or subdirectory) in the current directory, if it is a subdirectory, then enter the subdirectory and recursively call the function search_dirctory to find, after the lookup is completed, return to the previous directory; if Not a subdirectory but a file, it is to determine if it is the file we have to find, and if so, its complete file path is output. In this way, the entire directory can be implemented, including the traversal search of the subdirectory, including subdirectory. The examples will be described in detail below how to program the file search in the entire directory tree in VC .

1. A dialog-based application Search is created in the default manner in Visual C 6.0 (similar to VC 5.0. Place a command button on the main window dialog, its CAPTION is "Search File", ID is ID-Button-Search. Click this button to complete the file's lookup work.

2. Add a handle ONBUTTONSEARCH with a BN_Clicked event for the "Search File" button, the code is as follows:

#Include

#Include

......

Void CSearchdlg :: OnButtonSearch ()

{

// Todo: Add Your Control Notification Handler Code Here

Char szfilename [80];

// String SZFileName indicates the file name to find

STRCPY (SZFileName, "MyText.txt");

_CHDIR ("D: //"); // Enter the path to find (or for a specific directory)

/ / Find a file, if you check, the path is displayed.

Search_directory (SZFileName);

/ / A member of the CSEARCHDLG class

MessageBox ("Find Files!");

// Show information on the finished

}

3. Add a member function search_directory in the CSearchDLG class, which will complete the specific file lookup work, the code is as follows:

Void CSearchdlg :: Search_Directory (Char * SZFileName)

{

Long Handle;

Struct _finddata_t filestruct;

/ / Indicates information about the file (or directory)

CHAR PATH_SEARCH [_MAX_PATH];

/ / Indicates the path results found

// Start searching for work, find the first entity in the current directory (file or child directory),

// "*" means finding any file or subdirectories, filestruct is looking for results

Handle = _FindFirst ("*", & filestruct);

// If the handle is -1, it means that the current directory is empty, then the lookup is returned.

IF ((Handle == -1)) Return;

// Check if the first entity found is a directory (filestruct.name is its name)

IF (: getFileAttributes (filestruct.name) & file-attribute-directory)

{

// If it is a directory, enter the directory and recursively call the function search_dirctory to find, // Note: If the first character of the directory is '.' (Ie "." Or ".."), no search

IF (fileStruct.name [0]! = '.')

{

-Chdir (fileStruct.name);

Search_directory (SZFileName);

// After the lookup is completed, return to the previous directory

-Chdir ("..");

}

}

Else // If the first entity is not a directory, check if it is the file to look up.

{

// StricMP comparison with two strings, return to 0, expressed completely

IF (! Stricmp (fileStruct.name, szfilename))

{

// Get the full path to the current work directory

-Getcwd (path_search, -max-path);

// Receive the full path name of the file (including the name of the file)

Strcat (path_search, "//");

STRCAT (Path-Search, FileStruct.name);

Messagebox (path_search); // Output display

}

}

/ / Continue to perform the same look for the above in the current directory

While (! (- FindNext (Handle, & FileStruct))))))

{

IF (: getFileAttributes (filestruct.name) & file-attribute-directory)

{

IF (* filestruct.name! = '.')

{

-Chdir (fileStruct.name);

Search_directory (SZFileName);

-Chdir ("..");

}

}

Else

{

IF (! Stricmp (fileStruct.name, szfilename))

{

-Getcwd (path-search, -max-path);

Strcat (path_search, "//");

STRCAT (Path_Search, FileStruct.name);

Messagebox (Path_Search);

}

}

}

-FindClose (Handle);

// Finally end the entire lookup work

}

转载请注明原文地址:https://www.9cbs.com/read-48658.html

New Post(0)