#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);
}