拷贝所有文件的程序

360影视 2025-01-11 12:42 2

摘要:fnsplit(searchstr,sdrive,subdir,file,ext);

DOS时代编写的C程序

/* Name : COPYALL.C */

#include

#include

#include

#include

#include

#include

#include

#include

#define TRUE 1

#define FALSE 0

#define MaxRead 5000

voidCopyfile(void);

char searchstr[MAXPATH],

targdir[MAXPATH],

pathname[MAXPATH],

filename[MAXPATH],

sdrive[MAXDRIVE],

tdrive[MAXDRIVE],

subdir[MAXDIR],

file[MAXFILE],

ext[MAXEXT],

sele;

int month0,day0,year0;

long int total=0;

main(int argc,char *argv)

{

char origdir[MAXPATH],

rootdir[MAXPATH],

tempdir[MAXPATH],

right[80];

int i,len;

strcpy(right,"\n Copy ");

strcat(right,"All files, ");

strcat(right,"(");

strcat(right,"c");

strcat(right,")");

strcat(right,"C");

strcat(right,"o");

strcat(right,"p");

strcat(right,"y");

strcat(right,"r");

strcat(right,"i");

strcat(right,"g");

strcat(right,"h");

strcat(right,"t ");

strcat(right,"C");

strcat(right,"S");

strcat(right,"&");

strcat(right,"S");

strcat(right,"-");

strcat(right,"I");

strcat(right,"M ");

strcat(right,"1");

strcat(right,"9");

strcat(right,"9");

strcat(right,"4\n");

puts(right);

if(argc

{

printf(" Usage : COPYALL [d:][path]filename[.ext] [d:][path][/mm-dd-yy][/s]\n");

return(1);

}

else if(argc==2)

{

strcpy(tempdir,strupr(argv[1]));

i=delete(tempdir,"/S");

if(i==0) sele='S';

else sele=' ';

i=strcspn(tempdir,"/");

strncpy(searchstr,tempdir,i);

if(i==strlen(tempdir))

{

month0=0;

day0=0;

year0=0;

}

else

{

strtok(tempdir,"/");

month0=atoi(strtok(NULL,"-"));

day0=atoi(strtok(NULL,"-"));

year0=atoi(strtok(NULL,"-"));

if(year050) year0+=1900;

else if(year0

}

getcwd(targdir,MAXPATH);

}

else

{

strcpy(searchstr,strupr(argv[1]));

strcpy(tempdir,strupr(argv[2]));

if(i==0) sele='S';

else sele=' ';

strncpy(targdir,tempdir,i);

{

month0=0;

day0=0;

year0=0;

}

else

{

strtok(tempdir,"/");

}

}

fnsplit(searchstr,sdrive,subdir,file,ext);

sprintf(rootdir,"%s%s",sdrive,subdir);

sprintf(searchstr,"%s%s",file,ext);

len=strlen(rootdir)-1;

if(len>2) rootdir[strlen(rootdir)-1]='\0';

len=strlen(targdir)-1;

if(len>3) strcat(targdir,"\\");

fnsplit(targdir,tdrive,subdir,file,ext);

sprintf(targdir,"%s%s",tdrive,subdir);

if(len>2) targdir[strlen(targdir)-1]='\0';

getcwd(origdir,MAXPATH);

i=sdrive[0]-64;

setdisk(i-1);

getcwd(tempdir,MAXPATH);

chdir(rootdir);

copyfile;

if(total>0) printf("� file(s) copied",total);

chdir(tempdir);

setdisk(origdir[0]-65);

chdir(origdir);

return(0);

}

void copyfile(void)

{

char curdir[MAXPATH],

targname[MAXPATH],

buf[MaxRead],

temp[3];

int done,junk,attrib,Copyed,handle,numread,month,day,year;

float dsize,fsize;

FILE *fp1,*fp2;

struct ffblk dta;

struct ffblk dtat;

struct ftime filet;

struct dfree frespace;

getcwd(curdir,MAXPATH);

attrib=47;

done=findfirst(searchstr,&dta,attrib);

if(!done) printf("\npath: %s\n",curdir);

while(!done)

{

junk=dta.ff_fdate;

day=junk2;

junk=junk/32;

month=junk;

year=junk/16+1980;

if((year>year0)||(year==year0&&(month*100+day)==month0*100+day0))

{

sprintf(targname,"%s%s",targdir,dta.ff_name);

Copyed=TRUE;

if(!findfirst(targname,&dtat,attrib))

{

printf("File %s has been Existed. Overwrite(Y/N)?",dta.ff_name);

if(toupper(getche)=='Y') remove(targname);

else Copyed=FALSE;

printf("\n");

}

if(Copyed)

{

fsize=dta.ff_fsize;

getdfree(tdrive[0]-64,&frespace);

dsize=frespace.df_bsec*frespace.df_sclus;

dsize=dsize*frespace.df_avail;

while(dsize

{

printf("\nNo sufficient disk space,please change a disk\n");

printf("Press any key when ready");

getch;

printf("\n");

getdfree(tdrive[0]-64,&frespace);

dsize=frespace.df_bsec*frespace.df_sclus;

dsize=dsize*frespace.df_avail;

}

if((fp1=fopen(dta.ff_name,"rb"))==NULL)

{

printf("Can't open input file\n");

printf(" Usage : COPYALL [d:][path]filename[.ext] [d:][path][/mm-dd-yy][/s]\n");

return;

}

puts(targname);

if((fp2=fopen(targname,"wb"))==NULL)

{

printf("Can't open output file\n");

return;

}

getftime(fileno(fp1),&filet);

do{

numread=fread(buf,sizeof(char),MaxRead,fp1);

fwrite(buf,sizeof(char),numread,fp2);

}while(numread==MaxRead);

fclose(fp1);

fclose(fp2);

handle=open(targname,O_RDWR);

setftime(handle,&filet);

close(handle);

total++;

printf("%-12s is copied\n",dta.ff_name);

}

}

done=findnext(&dta);

}

if(sele=='S')

{

attrib=FA_DIREC+FA_SYSTEM+FA_HIDDEN;

done=findfirst("*.*",&dta,attrib);

while(!done)

{

strncpy(temp,dta.ff_name,2);

if(((dta.ff_attrib&FA_DIREC)==FA_DIREC)&&(temp[0]!='.'))

{

if(strlen(curdir)!=3) strcat(curdir,"\\");

strcat(curdir,dta.ff_name);

chdir(curdir);

copyfile;

chdir("..");

getcwd(curdir,MAXPATH);

}

done=findnext(&dta);

}

}

}

delete(char str,char substr)

{

int i,j,k,location=-1;

for(i=0;str[i]&&(location==-1);i++)

for(j=i,k=0;str[j]==substr[k];j++,k++);

if(!substr[k+1]) location=i;

if(location!=-1)

{

for(k=0;substr[k];k++);

for(j=location,i=location+k;str[i];j++,i++) str[j]=str[i];

str[j]='\0';

return(0);

}

else return(-1);

}

来源:左手牵起右手

相关推荐