Folder traversal in Windows

Source: Internet
Author: User

I think folder traversal is quite useful, so I wrote another traversal program based on titilima, which can display all the content in the folder.

The source program is as follows: (compiled by vc7.1)

The following content references titilima:

# Include <iostream>
# Include <cstdio>
# Include <queue>

# Include <windows. h>

Using namespace STD;

/*
* Traverse all files and folders in lpszpath and display the content in sequence.
*/
/*
* If a folder is scanned, it is saved to the dirs queue and the name is displayed,
* If a file is scanned, its name is displayed;
* After the current folder is processed, process the next level folder.
* Column.
*/

Void function (const lpctstr lpszpath)
{
// Start searching;

Queue <tchar *> dirs;

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 () <Endl;

// 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 it is not found or the search fails
{
Do
{
If (WFD. cfilename [0] = '.')
Continue; // filter the "." ".." Directories
If (WFD. dwfileattributes & file_attribute_directory)
{
Cout <"<dir>/t" <WFD. cfilename <Endl; // output directory name
Tchar szfile [max_path * 2];

Wsprintf (szfile, "% S // % s", dirs. Front (), WFD. cfilename );

// Function (szfile); // If a directory is found, go to this directory for Recursion
Tchar * TMP = new tchar [lstrlen (szfile) + 1];
Lstrcpy (TMP, szfile );
Dirs. Push (TMP );
}
Else
{
// Operate the object
Cout <'/t' <WFD. cfilename <Endl; // output file name
}
} While (findnextfile (hfind, & WFD ));
}

Delete [] dirs. Front ();
Dirs. Pop ();

Findclose (hfind); // close the search handle

}
}

Int main (INT argc, char * argv [])
{
If (argc <= 1)
{
Wcout <Endl <"folder traversal, enter the path :";
Tchar path [max_path];
Cin> path;
Function (PATH );
Wcout <Endl;
System ("pause ");
}
Else
{
Function (argv [1]);
}

}

Recursively write Win32 folder TraversalTitilima (original)

Folder traversal is a very useful technology, which is used in file search and anti-virus software. Below I will discuss how to implement this technology in Win32.
The core of the folder traversal technology is the use of recursive algorithms. I will not introduce Recursive Algorithms here. If you do not understand them, please refer to the relevant content.
The following is my pseudo-code:
Void function (lpctstr lpszpath)
{
Start searching;
If (File not found)
Return;
Do
{
If (the file found is a directory)
Function (directory found );
Else
Operations on files;
} While (find the next file and succeeded );
}
The API functions and struct used to implement this algorithm are:
· Findfirstfile;
· Findnextfile;
· Win32 _ find_data.
Now let's assume that you have understood the usage of the above functions and struct.
Now let's write the code to start searching. Before that, I first assume that the path format passed in by the function parameter lpszpath is X:/(root directory) or X:/Dir (non-root directory ), this is because the path format is usually used in Win32 programming. You must note that if the path is the root directory, there is a path separator "/" behind it. Otherwise, there is no. Therefore, when writing code, I must handle these two cases separately. The Code is as follows:
Tchar szfind [max_path];
Lstrcpy (szfind, lpszpath );
If (! Isroot (szfind) // isroot is a function compiled by myself. If the parameter is the root directory, true is returned.
Lstrcat (szfind ,"//");
Lstrcat (szfind, "*. *"); // find all files
Win32_find_data WFD;
Handle hfind = findfirstfile (szfind, & WFD );
If (hfind = invalid_handle_value) // if not found or the search fails
Return;
Next I will discuss what to do if the file is found. But before that, please enter the MS-DOS mode and enter dir press enter, what do you see?

Yes, DOS won't tell you the truth. It doesn't always hide important things like Windows. If you are not in the root directory, you will see ". "and"... "These two directories -- they are not visible in the resource manager. From the DOS era, I can see that one point represents the current directory, and the other two points represent the upper-level directories. When processing information, I must filter out the two. The reason is explained below. The do-while code is as follows:
Do
{
If (WFD. cfilename [0] = '.')
Continue; // filter these two directories
If (WFD. dwfileattributes & file_attribute_directory)
{
Tchar szfile [max_path];
If (isroot (lpszpath ))
Wsprintf (szfile, "% S % s", lpszpath, WFD. cfilename );
Else
Wsprintf (szfile, "% S // % s", lpszpath, WFD. cfilename );
Function (szfile); // If a directory is found, go to this directory for Recursion
}
Else
{
// Operate the object
}
} While (findnextfile (hfind, & WFD ));
Findclose (hfind); // close the search handle
Now I want to explain why we need to filter out the two directories with vertices. As you can see, if you find a directory, go to this directory for recursion-What if it is the current directory? The answer is obvious. If you do not filter it, the program will go to the "current directory" for recursion. Yes, it will lead to endless recursion.
There are so many algorithms. Because this is an algorithm that consumes system resources extremely, it is best to put it in a separate thread when you use it in your program, otherwise, your program will not respond in the search process.
Appendix: isroot function source code
Bool isroot (lpctstr lpszpath)
{
Tchar szrot [4];
Wsprintf (szrot, "% C: //", lpszpath [0]);
Return (lstrcmp (szrot, lpszpath) = 0 );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.