copyfile

#ifndef CFORDER_H
#define CFORDER_H

#include <windows.h>
#include <stdio.h>
#include <string>
#include <time.h> //clock()函数

using namespace std;
class CForder
{
public:
 CForder()
 {
  m_FilesNum=0;
 }
private:
 char m_Soupath[MAX_PATH];
 char m_Aimpath[MAX_PATH];

 void ParseFilePathName(char* Aimfile);  //把文件名字分割
 void CopyFiles(char* SourPathFile,char* AimPathfile,BOOL fBool);
public:
 int m_FilesNum;
 void CopyFiless(char* SourPathFile, char* AimPathfile,BOOL fBool);
};
#endif

 

//


#include "MyFileCopy.h"

void CForder::ParseFilePathName(char* Aimfile)  //把目标文件名字分割 创建文件夹
{
 char seps[]="//";
 char AimFiles[MAX_PATH];

 strcpy(AimFiles,Aimfile);

 char *Aim=strtok(AimFiles,seps);
 char CreAim[MAX_PATH];

 strcpy(CreAim,Aim);
 strcat(CreAim,"//");
 do
 {
  Aim=strtok(0,seps);
  if(Aim!=0)
  {
   strcat(CreAim,Aim);
   strcat(CreAim,"//");
   CreateDirectory(CreAim,NULL);
  }
  
 }while(Aim!=0);
 
}

void CForder::CopyFiles(char* SourPathFile,char* AimPathfile,BOOL fBool)//拷贝文件夹到目标文件夹
{
 WIN32_FIND_DATA   findData;
 HANDLE   hFindFile;

 char SourPaths[MAX_PATH];
 char AimPaths[MAX_PATH];

 strcpy(SourPaths,SourPathFile);
 strcat(SourPaths,"//*");

 
 hFindFile=::FindFirstFile(SourPaths,   &findData);
 if(hFindFile!=INVALID_HANDLE_VALUE)
 {
  do
  {
   if(findData.cFileName[0]== '.')
   {
    continue;
   }
   strcpy(AimPaths,AimPathfile);//目标路径
   strcat(AimPaths,findData.cFileName);
   
   strcpy(SourPaths,SourPathFile);
   strcat(SourPaths,findData.cFileName);
   
   if(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) //是文件夹
   {
     strcat(SourPaths,"//");
    strcat(AimPaths,"//");
    CreateDirectory(AimPaths,NULL);
    CopyFiles(SourPaths,AimPaths,fBool);
   }
   else//是文件
   {
    m_FilesNum++;
    CopyFile(SourPaths, AimPaths, fBool);
   }
  }while(::FindNextFile(hFindFile,&findData));
  ::FindClose(hFindFile);
 }
 else
 {
  printf(" %s /n", "can't open the file");
 }
 return ;
}

void CForder::CopyFiless(char* SourPathFile, char* AimPathfile,BOOL fBool)//如果是文件直接复制 如果是文件夹调用CopyFiles()
{

 char *aimfile=new char[MAX_PATH];
 HANDLE            hFindFile0;
 WIN32_FIND_DATA   findData0;
 hFindFile0=::FindFirstFile(SourPathFile,   &findData0);
 
 if(hFindFile0!=INVALID_HANDLE_VALUE)
 {  
  if(!(findData0.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) // SourPathFile 如果是文件复制
  {   
   BOOL f=CopyFile(SourPathFile, AimPathfile, fBool);   
   ::FindClose(hFindFile0);  
   return ;  
  } 
  else  //SourPathFile 如果是文件夹创建该文件夹
  {     
   ParseFilePathName(AimPathfile);//创建目标文件夹
   strcpy(aimfile,AimPathfile);    
   strcat(aimfile,"//");  
   strcat(aimfile,findData0.cFileName);   
   CreateDirectory(aimfile,NULL);    
  }
 }
 else
 {
  printf(" %s /n", "can't open the file");
 }
 
 strcpy(m_Soupath,SourPathFile);
 strcat(m_Soupath,"//");
 
 strcpy(m_Aimpath,aimfile);
 strcat(m_Aimpath,"//"); 
 delete []aimfile;
 CopyFiles(m_Soupath,m_Aimpath,fBool);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值