Click here to Skip to main content
15,920,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'd like to copy the contents of directory A to directory B (all sub-directories and files). Is there a simple statement that will do this? I have googled "copydir" (seemed like a good guess) but didn't get anything that looked like a straight answer. Thanks.
Posted

I think, short of a helper function, you'd have to go one-by-one on the files.

Thankfully, there's a helper function here on CP :) :
Copy, Move and Delete files and directories without using SHFileOperation[^]

Cheers.
 
Share this answer
 
Give a look to SHFileOperation [^]/ IFileOperation[^]
 
Share this answer
 
Thats easy:
C++
#pragma once
#include <stdio.h>
#include <tchar.h>
#include <windows.h>


// this: WalkFs
class vWalkFs
{
public: // vWalkFs
  virtual int  Enter(const TCHAR* full,WIN32_FIND_DATA* fd) = 0;
  virtual int  Leave(const TCHAR* full,WIN32_FIND_DATA* fd) = 0;

};

unsigned int scopy(TCHAR* dst,const unsigned int cbdst,const unsigned int pos,const TCHAR* src)
{
  unsigned int  ix,len = _tcslen(src);
  if((pos+len)<cbdst)
  {
    for(ix=0;ix<len;ix++) dst[pos+ix]=src[ix]; dst[pos+ix]=0;
    return pos+len;
  }
  return 0;
}

static void __WalkFs(TCHAR* path,const unsigned int cb,const unsigned int plen,vWalkFs& walk)
{
  typedef struct
  {
    static inline int isdir(WIN32_FIND_DATA& fd)
    {
      return FILE_ATTRIBUTE_DIRECTORY & fd.dwFileAttributes;
    }
    static inline int isvalid(WIN32_FIND_DATA& fd)
    {
      if(!(FILE_ATTRIBUTE_DIRECTORY & fd.dwFileAttributes)) return 2;
      if('.'!=fd.cFileName[0]) return 1;
      if(0==fd.cFileName[1]) return 0; 
      if('.'!=fd.cFileName[1]) return 1; 
      if(0==fd.cFileName[2]) return 0; 
      return 1;
    }
  }_;

  WIN32_FIND_DATA  fd;
  HANDLE          hf;
  unsigned int    flen;

  scopy(path,cb,plen,__TEXT("\\*.*"));
  hf = FindFirstFile(path,&fd);
  if(INVALID_HANDLE_VALUE!=hf)
  {
    do
    {
      if(  _::isvalid(fd) )
      {
        flen = scopy(path,cb,plen+1,fd.cFileName);
        if(walk.Enter(path,&fd))
        {
          if(_::isdir(fd)) __WalkFs(path,cb,flen,walk);
          walk.Leave(path,&fd);
        }
      }
    } while(FindNextFile(hf,&fd));
    FindClose(hf);
  }
  path[plen] = 0;
}

void WalkFs(vWalkFs& walk,const TCHAR* path)
{
  TCHAR          full[_MAX_PATH+_MAX_FNAME+0x400];
  unsigned int  cbf = sizeof(full)/sizeof(full[0]);
  unsigned int  flen = scopy(full,cbf,0,path);
  if(flen && ('\\'==full[flen-1])) --flen;
  __WalkFs(full,cbf,flen,walk);
}


////////////////////////
// main
int _tmain(int argc, _TCHAR* argv[])
{
  TCHAR    src[0x1000];
  if(2<argc)
  {
    class w : public vWalkFs
    {
    public: // vWalkFs
      virtual int  Enter(const TCHAR* full,WIN32_FIND_DATA* fd)
      {
        scopy(dst,sizeof(dst)/sizeof(dst[0]),len,full+rel);
        if(FILE_ATTRIBUTE_DIRECTORY & fd->dwFileAttributes)
        {
          CreateDirectory(dst,0);
        }
        else
        {
          _tprintf(__TEXT("copy: %s -> %s\r\n"),full,dst);
          CopyFile(full,dst,0);
        }
        return 1;
      }
      virtual int  Leave(const TCHAR* full,WIN32_FIND_DATA* fd)
      {
        return 1;
      }
    public:
      w(const TCHAR* path)
      {
        len = scopy(dst,sizeof(dst)/sizeof(dst[0]),0,path);
        rel = 0;
        CreateDirectory(dst,0);
      }
      TCHAR          dst[0x1000];
      unsigned int  len;
      unsigned int  rel;
    }  walk(argv[2]);

    walk.rel = scopy(src,sizeof(src)/sizeof(src[0]),0,argv[1]);
    WalkFs(walk,src);
    _tprintf(__TEXT("<key>")); _gettch();

  }
  return 0;
}

Good luck.
 
Share this answer
 
v2

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