/* img_ren.c, 2009 Feb 19 * (C) Copyright Prem Sobel, 2009. All Rights Reseeved. * * This program renames all image files of types: * *.jpg, *.gif, *.png in a directory using a prefix. */ #include #include #include #include #define SIZE 20 static char name[SIZE], fn_new[SIZE+10]; static struct _finddata_t fileinfo; static char *prefix; static char Usage[] = "Usage: img_ren [prefix]\n"; static int k; void fail(char *msg) { fprintf(stderr,"*** %s\n",msg); exit(1); } // fail static do_rename(char *fn_old, char *ext) { sprintf(fn_new, "%s%03d.%s", prefix, k, &ext[2]); //printf("old=%s new=%s\n", fn_old, fn_new); rename(fn_old, fn_new); } // do_rename static void do_extension(char *ext) { long h; h = _findfirst(ext, &fileinfo); if(h>=0) { k = 1; do_rename(fileinfo.name, ext); for(k++; !_findnext(h, &fileinfo); k++) { do_rename(fileinfo.name, ext); } } _findclose(h); } // do_extension int main(int argc, char* argv[]) { int n; if(1 == argc) { printf("Enter prefix: "); if(!fgets(name, SIZE, stdin)) fail(Usage); prefix = name; } else if(2 == argc) { prefix = argv[1]; } else { fail(Usage); } printf("@ prefix=%s\n", prefix); for(n=0; prefix[n]; n++) { if(('\n' == prefix[n]) || ('\r' == prefix[n])) { prefix[n] = 0; break; } } do_extension("*.jpg"); do_extension("*.gif"); do_extension("*.png"); return 0; } // main