MFC uses the CFileFind class to recursively replicate folders (MFC file operation 2)

Source: Internet
Author: User

The CFileFind class has been mentioned in the previous blog and is used to delete any folder.

 

One problem during file replication is that I have been entangled for a long time. The member functions of the CFileFind class, GetFilePath () and GetFileName (), are actually two very well understood functions, one is the file path and the other is the file name. However, I made a mistake in understanding that the file path does not contain the file name, such as the file C:/Test/1.txt, so what is its path and name? I understood it as the path: C:/Test name 1.txt. I wrote the implementation code of this copy function according to my understanding. The teacher made an error and finally I found out, the original path contains the file name, that is, the above file path is: C:/Test/1.txt.

 

It seems to be a very low-level error, but it does bother me for a while.

 

Return to the topic.

 

The specific implementation idea is similar to the previous blog post "deleting a non-empty folder in MFC"

 

Please refer to the source code:

Void myCopyDirectory (CString source, CString target) <br/>{< br/> CreateDirectory (target, NULL ); // create the target folder <br/> // AfxMessageBox ("create folder" + target); <br/> CFileFind finder; <br/> CString path; <br/> path. format ("% s /*. * ", source); <br/> AfxMessageBox (path); <br/> bool bWorking = finder. findFile (path); <br/> while (bWorking) {<br/> bWorking = finder. findNextFile (); <br/> AfxMessageBox (finder. getFileName (); <br/> if (Finder. IsDirectory ()&&! Finder. isDots () {// is a folder and the name does not include. or .. <br/> myCopyDirectory (finder. getFilePath (), target + "/" + finder. getFileName (); // recursively create a folder + "/" + finder. getFileName () <br/>}< br/> else {// indicates that the file is copied directly <br/> // AfxMessageBox ("copy file" + finder. getFilePath (); // + finder. getFileName () <br/> CopyFile (finder. getFilePath (), target + "/" + finder. getFileName (), FALSE); <br/>}< br/>}

 

The source code is not hard to understand.


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.