C + + Traverse folder and its subfolders (clips)

Source: Internet
Author: User
Tags parent directory

The code is as follows

int _finder (LPCTSTR pstr)
{
CFileFind Finder;
CString Strwildcard (PSTR);
Strwildcard + = _t ("//*.*");

BOOL bworking = Finder. FindFile (Strwildcard);

int i = 0;
while (bworking)
{
bworking = Finder. FindNextFile () & Finder.isdots () & Finder. Isdirectory ();
Nonzero if the found file has the name "." or "...", which indicates that the found file is actually a directory. otherwise 0
/*
bworking = Finder. FindNextFile ();
if (!bworking)
break;
*/
If a file exists, execute the following
bworking = Finder. FindNextFile ();
if (finder. Isdots ())//if yes. Or.. Just continue.
{
bworking = Finder. FindNextFile ();
Continue;
}
General files and folders

printf ("%d%s/n", ++i, finder.) GetFileName ());
BOOL bisdir = Finder. Isdirectory ();
if (Bisdir)
{//is a folder
CString Repath = Finder. GetFilePath ();
_finder (Repath);
}
Else
{//is a file

Do parse
CString Repath = Finder. GetFilePath (); eg return c:/myhtml/myfile.txt



}


}//end while
Finder. Close ();
return 1;
}

There are the following points that may not be understood by people,

What is the use of isdots?

Isdots is to determine if the current file is. Or. files, each folder under Windows has these two files, ... Represents the parent directory,. Represents the current directory, desktop Windows is not visible, the following methods can be seen. , go to the command line and enter Dir to display all the directories under the current folder, and you will also find that. , the program must remove these two when running to prevent endless loop

You may encounter less reading a file, I made such a mistake in the beginning, I thought it was a bug in MS, it was my bug~

Your findnextifle must not be seated by number.

May be while (Finder.filenextfile ()) so written, you missed the first file, do not believe to try ~

And everyone wrote the traversal when the last file was found missing,

I guess that's what you wrote.

bworking = Finder.findnextfile ();

while (bworking)

{

bworking = Finder.findnextfile ();

Do something

......

}

This means that the last file is missing.

Missing the file that is logic problem ~ Good fix ~

Traversing the file in a fixed order, look at the reference body

Over the

Related Article

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.