怎样复制 A目录中与B目录下文件名相同的文件到新目录?

论坛里回复别人的内容。觉得或许以后自己也用得上,,, 记得有一次和lp一起挑选两个人都喜欢的照片。。。 ^^

anyway...

 

 

/*------------------------------------------------------

  Name: copy same files


  Author: fnfn. dubiousway 2010/4/12


  Desc: A simple program to copy files in dirA to a new dirC

        only if those files also in dirB

-------------------------------------------------------*/

#include <stdio.h>
#include <string.h>
#include<io.h>

bool fileCopy(char* destDir,char* sourFilePath);
int main(){

    _finddata_t fileDirA, fileDirB;
    long lfDirA, lfDirB;
    char* pathDirA="D://XYZ//D1",//A 目录
          pathDirA2[128],
        * pathDirB="D://XYZ//D2",//B 目录
        * pathCopyTo="D://XYZ//D3",// 复制 A 和 B 中相同的到这个目录
        fileName[128];
    int len;
    strcpy(pathDirA2, pathDirA);
    len= strlen(pathDirA2);
    if( pathDirA2[len-1]!=0x5c){
        pathDirA2[len]= 0x5c;
        pathDirA2[len+1]=   0;
    }
    strcat(pathDirA2,"*.*");
    if((lfDirA = _findfirst(pathDirA2,&fileDirA))==-1l)
        printf("No file is found/n");
    else{
        printf("Same files list:/n");
        do{
            if(fileDirA.name[0]=='.') continue;
            strcpy(fileName, pathDirB);
            len= strlen(fileName);
            if( fileName[len-1]!=0x5c){
                fileName[len]= 0x5c;
                fileName[len+1]=   0;
            }
            strcat(fileName, fileDirA.name);
            if( (lfDirB= _findfirst(fileName, &fileDirB))!= -1){
                printf("The following file is to be copied: %s/n",fileDirA.name);
                if(fileCopy(pathCopyTo, fileName))
                    printf("The file has been successfully copied./n");
            }
        }while( _findnext( lfDirA, &fileDirA ) == 0 );
    }
    _findclose(lfDirA);

    return 0;
}

bool fileCopy(char* destDir,char* sourFilePath){
    FILE* fpd, * fps;
    int numRead;
    char *pName= sourFilePath+strlen(sourFilePath),
         destFilePath[128],
         buf[1024];
    while(*--pName!= 0x5c);
    strcpy(destFilePath, destDir);
    if(*(destDir+strlen(destDir)-1)==0x5c)
        destFilePath[strlen(destDir)-1]=0;
    strcat(destFilePath, pName);

    fpd=fopen(destFilePath,"wb");
    fps=fopen(sourFilePath,"rb");
    if(!fpd || !fps){
        printf("File open error!/n");
        return false;
    }
    while(numRead= fread(buf, 1, 1024, fps))
        fwrite(buf,1,numRead,fpd);
    fclose(fpd), fclose(fps);
    return true;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值