Solved

copy a directory

Posted on 2003-10-23
11
257 Views
Last Modified: 2013-11-20
I want to copy a whole directory(folder) with all its files to another path or directory or CD.  Need complete code. I'm using MFC in WinNT.
0
Comment
Question by:rpatna
  • 6
  • 5
11 Comments
 
LVL 23

Accepted Solution

by:
Roshan Davis earned 50 total points
ID: 9604865

DWORD Copy_File(LPCSTR pszFrom, LPCSTR pszTo)
{
      SHFILEOPSTRUCT sFileOp;
      ZeroMemory(&sFileOp, sizeof(SHFILEOPSTRUCT));

      sFileOp.wFunc = FO_COPY;
      sFileOp.pFrom = pszFrom;
      sFileOp.pTo = pszTo;
      sFileOp.fFlags = FOF_SILENT|FOF_NOCONFIRMATION|FOF_NOERRORUI;

      if ( SHFileOperation(&sFileOp) )
      {
            return GetLastError();
      }

      return 0;
}
0
 

Author Comment

by:rpatna
ID: 9605336
Thanks , it is working fine.

now tell me how to remove full directory?
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9605398
DWORD DelTree(LPCSTR pszBase)
{
      SHFILEOPSTRUCT sFileOp;
      ZeroMemory(&sFileOp, sizeof(SHFILEOPSTRUCT));

      sFileOp.wFunc = FO_DELETE;
      sFileOp.pFrom = pszBase;
      sFileOp.fFlags = FOF_SILENT|FOF_NOCONFIRMATION|FOF_NOERRORUI;

      if ( SHFileOperation(&sFileOp) )
      {
            return GetLastError();
      }

      return 0;
}

Good Luck
0
 

Author Comment

by:rpatna
ID: 9605595
Thanks roshmon, it is working fine.

0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9605609
Welcome :o)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:rpatna
ID: 9632933
Hi roshmon,

now I'm getting one more problem.

let I'm writing the following code to copy folder

pszFrom = "e:\\Decom\\folder1";
pszTo     = "f:\\Decom";

this code working fine but if I write like :
CString tmpString1,tmpString2;

tmpString1 = "e:\\Decom\\folder1";
tmpString2 = "f:\\Decom";
...
...
pszFrom = tmpString1;
pszTo     = tmpString1;

means if I store pszFrom = some CString variable
or I tried strcpy(pszFrom,charString);
then it is not working

what to do ?





     

0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9632994
use sprintf
0
 

Author Comment

by:rpatna
ID: 9633481
sprintf  showing error:
can not convert parameter1 from "const char *" to "char *"

while debugging in following code, pszFrom showing "e:\\Decom\\folder1"
 but not copying

tmpString1 = "e:\\Decom\\folder1";
...
...
pszFrom = tmpString1;

what to do????
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9633538
Cast like this

(LPCTSTR)(LPTSTR)csSTRING
0
 

Author Comment

by:rpatna
ID: 9633957

in pszTo     = anything like CString or str or "path"
is working
but
pszFrom  = anything except "direct path"  not working.

type cast also not working.


0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9639188
Show me how u declared pszTo, and pszFrom

Rosh :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how do i create updater to My Activex application? 3 79
Complete beginner needs help making a cron job 9 110
post4 challenge 28 93
fix34  challenge 9 105
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now