I think the traversal of the folder is useful, so another traversal program is written on the basis of TitiLima, which can display all the contents under the folder.
The source program is as follows: (Compilation under VC7.1)
The following is the reference to Titilima's content:
#include
#include
Using namespace std;
/ * * Traverse all files and folders under LPSZPath and display the contents in order. * // * * If you scan to the folder, save it in the DIRS queue, and display the name, * If you scan it to the file, Then there is a name; * After the current folder is processed, the next level folder is handled, and the next level folder is obtained from the team * column. * /
Void function (const lpctstr lpszpath) {// Start search;
Queue
TCHAR * TMP = New Tchar [LSTRLEN (LPSZPATH) 1]; LSTRCPY (TMP, LPSZPATH);
IF (TMP [LSTRLEN (TMP) -1] == '//') TMP [LSTRLEN (TMP) -1] = '/ 0';
DIRS.PUSH (TMP);
Tchar Szfind [MAX_PATH]; for (;! DIRS.EMPTY ();) {cout << Endl << "in Directory ->" << DIRS.FRONT () <<
// LSTRCPY (SZFIND, DIRS [I]); LSTRCPY (SZFIND, DIRS.FRONT ()); LSTRCAT (SZFIND, "//*.*"); // find all files Win32_find_data WFD;
Handle Hfind = FindfirstFile (Szfind, & WFD); // Cout << Szfind << Endl;
If (hfind! = invalid_handle_value) // If you do not find or look for {DO {if (wfd.cfilename [0] == ') Continue; // filtering "" ".." these two directories IF ( Wfd.dwfileAttributes & file_attribute_directory) {cout << "
WSPrintf (SZFILE, "% s //% s", DIRS.FRONT (), WFD.cfileName);
// function (SZFILE); // If you find a directory, enter this directory to recurrent TCHAR * TMP = New Tchar [lstrlen (SZFILE) 1]; LSTRCPY (TMP, SZFILE); DIRS.PUSH (TMP); } Else {// Operate the file COUT << '/ t' << wfd.cfilename << Endl; // Output file name}} while (FindNextFile (Hfind, & WFD));} delete [] DIRS.FRONT ); Dirship.pop (); findclose (hfind); // Turn off the handle
}
INT main (int Argc, char * argv []) {if (argc <= 1) {wcout << Endl << "folder traversal, please enter the path:"; tchar path [max_path]; cin >> path; function (PATH); WCOUT << Endl; System ("pause");} else {function (argv [1]);}}
Handle Win32 folder traversed Titilima (original)
Folder traversal technology is a very useful technology that uses this technology in the file search and anti-virus software. I will discuss how to implement this technology in Win32. The core of folder traversal technology is to use the recursive algorithm. I will not introduce the recursive algorithm. If you don't understand, please refer to the relevant content. The following is my algorithm pseudo code: void function (LPCTSTR LPSZPATH) {start looking for; if (no file found) return; do {if (found by the file) Function (found directory); ELSE While (looking for the next file and success);} The API function and the structure that implementing this algorithm and the structure are: · FindFirstFile; · FindNextFile; · Win32_find_data. Here I assume that you have understood the above functions and structures, now let's begin. Now I will write the code that starts looking for. Prior to this, I first assume that the function parameter LPSZPath passed into the path format is X: / (root directory) or x: / div (non-root), because the Win32 programming is usually used in this path format. You must notice that if the path is the root directory, there is a path separator "/" behind it, no. Then I have to handle these two situations when I write code. This code is as follows: tchar szfind [max_path]; lstrcpy (szfind, lpszpath); if (! Isroot (szfind)) // isroot is a function I have written in my own, and if the parameter is the root directory, return TruelStrcat (Szfind, "/ / "); LSTRCAT (SZFIND," *. * "); // find all files WIN32_FIND_DATA WFD; Handle Hfind = FindFirstFile (Szfind, & WFD); if (HFIND == Invalid_Handle_Value) // If you do not find or find failed Return; Below I will discuss what if I find a file, what should I do. However, before this, please enter the MS-DOS method, and enter DIR Enter, what did you see?