#include #include #include #include #include #include #include int digits(char *name){ int r=0; while(isdigit(*name)){ r++; name++; } return r; } void procFile(char *root, char *oldname, time_t mtime){ char st[PATH_MAX], *cp, newname[PATH_MAX]; int c; struct tm *t; cp=strrchr(oldname,'.'); if(!cp) return; cp++; strcpy(st,cp); cp=st; while(*cp){ (*cp)=tolower(*cp); cp++; } //printf("Endung=%s\n",st); if(strcmp(st,"pdf")) return; c=digits(oldname); //printf("%d Ziffern\n",c); if(c==8) return; t=localtime(&mtime); sprintf(st,"%s/%s",root,oldname); sprintf(newname,"%s/%04d%02d%02d_%s",root,t->tm_year+1900, t->tm_mon+1, t->tm_mday,oldname); printf("%s -> %s\n",st,newname); rename(st,newname); } void procDir(char *root){ DIR *di; struct dirent *d; struct stat sb; char st[PATH_MAX]; //printf("verarbeite Verzeichnis %s\n",root); di=opendir(root); if(!di) return; while(d=readdir(di)){ if(d->d_name[0]!='.'){ sprintf(st,"%s/%s",root,d->d_name); stat(st,&sb); if((sb.st_mode & S_IFMT)==S_IFDIR){ //printf("Verzeichnis %s\n",st); procDir(st); } if((sb.st_mode & S_IFMT)==S_IFREG){ //printf("Datei %s\n",st); procFile(root, d->d_name,sb.st_mtim.tv_sec); } } } closedir(di); } int main(int argc, char **argv){ if(argc<2) return -1; procDir(argv[1]); printf("fertig.\n"); return 0; }