Click here to Skip to main content
Licence 
First Posted 6 Jul 2006
Views 21,934
Bookmarked 12 times

Copy Files And Directories By Traversing

By | 6 Jul 2006 | Article
Create directories and copy files by traverse function

Introduction

I have searched many sites in Google but was not lucky enough to find a code snippet which is used for copying files using traverse algorithm. I got it after a little effort and I want to share it with friends out there. It is simple and I am including the code snippet below. This is my first submission here.

The Code

This comes under the button click:

void CCopyFilesFrmFoldersDlg::OnButCopy() 
{ 
 CString csSourceDir(_T("")), csDestDir(_T("")); 
 UpdateData(TRUE); 
 GetDlgItemText(IDC_EDIT_SOURCE, csSourceDir);
 GetDlgItemText(IDC_EDIT_DEST, csDestDir); 
 CopyDirAndFiles(csSourceDir, csDestDir);
}

This goes in the function CopyDirAndFiles:

bool CCopyFilesFrmFoldersDlg::CopyDirAndFiles(CString csSource, CString csDest)
{
 bool bReturn  = false; 
 
 try
 {
  CFileFind oFileFind; 
  CString csSourceFilePath(""),csDestFilePath("");
  int nSym(0), nTot(0);
  CString csTemp(""); 
  csSourceFilePath = csSource;
  csSourceFilePath += "\\*.*";
  
 /////////start looping////////
  BOOL bWorking = oFileFind.FindFile(csSourceFilePath);
  
  while (bWorking)
  {   
   ////////////Search for next file///
   bWorking = oFileFind.FindNextFile(); 
   ///////////File Path Name//////////
   csSourceFilePath = oFileFind.GetFilePath(); 
   if(oFileFind.IsDirectory())
   {      
    if(oFileFind.GetFileName().CompareNoCase(".")==0||
		oFileFind.GetFileName().CompareNoCase("..")==0)
    continue;   
    csDestFilePath = csDest;
    nSym = csSourceFilePath.ReverseFind('\\');
    nTot = csSourceFilePath.GetLength();    
    csTemp = csSourceFilePath.Right(nTot - nSym);    
    csDestFilePath += csTemp; 
    CreateDirectory(csDestFilePath, NULL); 
    CopyDirAndFiles(csSourceFilePath, csDestFilePath);
   }
   else
   {    
    csDestFilePath = csDest;
    nSym = csSourceFilePath.ReverseFind('\\');    
    nTot = csSourceFilePath.GetLength();    
    csTemp = csSourceFilePath.Right(nTot - nSym);    
    csDestFilePath += csTemp;
    
    CopyFile(csSourceFilePath, csDestFilePath, FALSE);
   }
  }
  
  oFileFind.Close();  
  bReturn = true;
 } 
 
 catch(...)
 { 
  bReturn = false;
 } 
 
 return bReturn;
}

Hope it helps...

History

  • 6th July, 2006: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

maneshcs

Web Developer

India India

Member

I am a software engineer working in VC++ for the past 2 years. Have expertise in MFC, ACTIVEX, COM. Have done a couple of projects in C# .net(windows) and also had my hands on C# web services.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMicrosoft is a criminal institution PinmemberYahzi15:55 21 Sep '06  
GeneralRe: Microsoft is a criminal institution PinmemberSKhokalay20:16 21 Sep '06  
GeneralRe: Microsoft is a criminal institution Pinmemberpointer9:08 28 Aug '08  
GeneralRe: Microsoft is a criminal institution PinmemberAmVal6:02 7 Nov '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 6 Jul 2006
Article Copyright 2006 by maneshcs
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid