Windows文件夹拷贝


// 函数名: CopyDirectory
// 参数1: RegularExpression 正则表达式
// 参数2: SourceDirectory 源目录
// 参数3: DestinationDirectory 目标目录
// 功能: 把源目录中符合正则表达式的文件按照相同的结构复制到目标目录
// 调用示例: CopyDirectory("\\*","C:\\Test Directroy","D:\\Test Directroy");

void CopyDirectory(LPCTSTR RegularExpression, LPCTSTR SourceDirectory, LPCTSTR DestinationDirectory)
{
 WIN32_FIND_DATA wfd = {0};
 HANDLE hFindFile = NULL;
 char *FileName = (char *)malloc( MAX_PATH );
 strcpy(FileName,SourceDirectory);
 strcat(FileName,RegularExpression);

 SECURITY_ATTRIBUTES attribute;
 attribute.nLength = sizeof(attribute);
 attribute.lpSecurityDescriptor = NULL;
 attribute.bInheritHandle = FALSE;
 CreateDirectory(DestinationDirectory,&attribute);

 hFindFile = ::FindFirstFile(FileName,&wfd);
 free(FileName);
 if(INVALID_HANDLE_VALUE!=hFindFile)
 {
  do
  {
   if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
   {
    if(strcmp(wfd.cFileName,".")&&strcmp(wfd.cFileName,".."))
    {
     char *NextSourceDirectory = (char *)malloc( MAX_PATH );
     strcpy(NextSourceDirectory,SourceDirectory);
     strcat(NextSourceDirectory,"\\");
     strcat(NextSourceDirectory,wfd.cFileName);

     char *NextDestinationDirectory = (char *)malloc( MAX_PATH );
     strcpy(NextDestinationDirectory,DestinationDirectory);
     strcat(NextDestinationDirectory,"\\");
     strcat(NextDestinationDirectory,wfd.cFileName);

     CopyDirectory(RegularExpression,NextSourceDirectory,NextDestinationDirectory);

     free(NextSourceDirectory);
     free(NextDestinationDirectory);
    }
   }
   else
   {
    char *SourceFile = (char *)malloc( MAX_PATH );
    strcpy(SourceFile,SourceDirectory);
    strcat(SourceFile,"\\");
    strcat(SourceFile,wfd.cFileName);

    char *DestinationFile = (char *)malloc( MAX_PATH );
    strcpy(DestinationFile,DestinationDirectory);
    strcat(DestinationFile,"\\");
    strcat(DestinationFile,wfd.cFileName);

    FILETIME lpCreationTime,lpLastAccessTime,lpLastWriteTime;
    HANDLE hFile = CreateFile(DestinationFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
    if(hFile!=INVALID_HANDLE_VALUE)
    {
     if(GetFileTime(hFile, &lpCreationTime, &lpLastAccessTime, &lpLastWriteTime))
     {
      CloseHandle(hFile);
      if(lpLastWriteTime.dwHighDateTime==wfd.ftLastWriteTime.dwHighDateTime&&lpLastWriteTime.dwLowDateTime==wfd.ftLastWriteTime.dwLowDateTime)
      {
       //修改时间相同,不用替换
       continue;
      }
     }
     CloseHandle(hFile);
    }

    if(!CopyFile(SourceFile,DestinationFile,0))
    {
     //拷贝失败
    }
    else
    {
     //拷贝成功
    }
    free(SourceFile);
    free(DestinationFile);
   }
  }while(::FindNextFile(hFindFile,&wfd));
  ::FindClose(hFindFile);
 }
}

 

你可能感兴趣的:(WinCE,Windows)