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.