怎樣複製 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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章