论坛里回复别人的内容。觉得或许以后自己也用得上,,, 记得有一次和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;
}