Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hell,I'm writing an application.part of it is about copy all word files(*.doc) in D disk to E disk in MFC.But I find the code is not satisfying.
code
C++
void CMOVEDlg::CopyFile(CString destdir)//destinaton directoty is E
{
    CFileFind tempFind;
    CString tempFileFind;
    tempFileFind.Format("%s\\*.doc",destdir);
    BOOL IsFinded=(BOOL)tempFind.FindFile(tempFileFind);
    while(IsFinded)://when no word file(*.doc) in the based directoty in D, bWorking is wrong and can't come into the while loop.But I want to find and copy all doc file in D.What should I do?

    {
      IsFinded=(BOOL)tempFind.FindNextFile();
      if(!tempFind.IsDots())  
      {
         CString foundFileName;
         foundFileName=tempFind.GetFileName();

         if(tempFind.IsDirectory())
         {
            CString tempDir;
            tempDir.Format("%s\\%s",destdir,foundFileName);
            CopyFile(tempDir);
         }
         else
         {
            CString tempFileName1,tempFileName2;
            tempFileName1.Format("%s\\%s",dir,foundFileName);
            tempFileName2.Format("%s\\%s",m_Path,foundFileName);
            ::CopyFile(tempFileName1,tempFileName2,FALSE);
         }
      }
    }
    tempFind.Close();
}


Could someone help me improve the code? Thanks in advance.
Posted
Updated 11-Mar-11 8:35am
v2

1 solution

Your logic is wrong. You should not be determining bailout based on if *.doc files have been found or not.

CopyFile should have no logic in it that searches for files to copy. It should do only and exactly what the function name says it does.

You should add another method that searches the specific folder for *.doc files, then calls itself for any subfolders it finds. When *.doc files are found, you can pass those filepaths to a CopyFile for obvious reasons.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900