/* Pro136F.c */
/* "6Rename.Exe" */
/* Automatic Rename Batch generator as a Finalizing polish.
	  The Batch will return Original Long foldernames.        */
/* Read  'LOOKUP.TXT'                           */
/* Write 'RENAME.BAT'                           */
/* This C program is a variation of 'Pro136C.c' which automatically
	  generate DOS-Batch program after it captures Directory names.  */
/* (2005-02-06) I realized that I can't make the RENAME.BAT
 *  directly from "DIR.TXT", but I should read from "LOOKUP.TXT"
 *  which tells,
 *    catlg001.txt  <=>  The first folder_name.
 *    catlg002.txt  <=>  The second folder_name.
 *    catlg003.txt  <=>  The third folder_name.
 */

#include <stdio.h>
#include <string.h>

char inline [256];
char id [128], originalName [128];

extern char *firstwd (char *to, char *from);   /* Recycling Object 'firstwd()' */
extern char *spacedwd (char *to, char *from);    /* Modified Recycling Object */

void main(void){
   char strPause[16];
   FILE *infp, *outfp;
   /*  Actually, "LOOKUP.TXT" is simpler */

   infp=fopen("LOOKUP.Txt", "r");
   outfp=fopen("RENAME.BAT", "w");

/*  I can write a set of Five RENAME here at different locations, */
/*  while RENAME command lines are jumping around Five folders all the time. */
/*  by "Change Directory (CD)".  I tried the latter way and it is just fine. */
/*  Without Changing Directory, the DOS-Batch command didn't work.           */

   /* Only the First Line, it is current directory, no "CD .." at first. */
   fgets(inline, 256, infp);
   firstwd(id, &inline[2]);
   spacedwd(originalName, &inline[18]);
   fprintf (outfp, "CD SDW\n");
   fprintf (outfp, "RENAME %s.Txt   \"%s.Txt\"\n", id, originalName);
   fprintf (outfp, "CD ..\\ZIP_FL\n");
   fprintf (outfp, "RENAME %s.Txt   \"%s.Txt\"\n", id, originalName);
   fprintf (outfp, "CD ..\\SDW\\Erased\n");
   fprintf (outfp, "RENAME %s.Txt   \"%s.Txt\"\n", id, originalName);
   fprintf (outfp, "CD ..\\..\\ZIP_FL\\Erased\n");
   fprintf (outfp, "RENAME %s.Txt   \"%s.Txt\"\n", id, originalName);
   fprintf (outfp, "CD ..\\..\\DIFF\n");
   fprintf (outfp, "RENAME %s.Txt   \"%s.Txt\"\n", id, originalName);

   /* The Second Line and Later */
   while(fgets(inline, 256, infp)!=NULL){
		 firstwd(id, &inline[2]);  spacedwd(originalName, &inline[18]);
		 fprintf (outfp, "CD ..\\SDW\n");
		 fprintf (outfp, "RENAME %s.Txt   \"%s.Txt\"\n", id, originalName);
		 fprintf (outfp, "CD ..\\ZIP_FL\n");
		 fprintf (outfp, "RENAME %s.Txt   \"%s.Txt\"\n", id, originalName);
		 fprintf (outfp, "CD ..\\SDW\\Erased\n");
		 fprintf (outfp, "RENAME %s.Txt   \"%s.Txt\"\n", id, originalName);
		 fprintf (outfp, "CD ..\\..\\ZIP_FL\\Erased\n");
		 fprintf (outfp, "RENAME %s.Txt   \"%s.Txt\"\n", id, originalName);
		 fprintf (outfp, "CD ..\\..\\DIFF\n");
		 fprintf (outfp, "RENAME %s.Txt   \"%s.Txt\"\n", id, originalName);
   }
   fclose (infp);
   fclose (outfp);

}