home *** CD-ROM | disk | FTP | other *** search
/ synchro.net / synchro.net.tar / synchro.net / modem.madness / SMMNETML / QNAME2.ZIP / QNAME2.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-22  |  4.1 KB  |  134 lines

  1. /*  QNAME2.C
  2. =========================================================================
  3.  
  4.     Original program -- QNAME.C
  5.  
  6.     QNAME.C -- renames ATBBS.QWK in current directory to new file name
  7.     that contains current day and month.
  8.     Written by Martin Leon, Ashton Tate Software Support using the
  9.     Microsoft Quick C compiler.
  10.  
  11. =========================================================================
  12.  
  13.     QNAME.C modified to QNAME2.C
  14.     by David J. Semon
  15.  
  16.     March 22, 1991
  17.  
  18.     Program modified to:
  19.  
  20.     1. Compile with Borland Turbo C 2.0 (struct date d) vs Microsoft
  21.        Quick C (dosdate_t today).
  22.     2. Allow any file name to be entered as a command line argument.
  23.     3. Rename the input file to a new file name that contains the first
  24.        three characters of the input file name plus current day, month
  25.        and a sequence letter (A,B,C,D, etc.).
  26.  
  27.        Examples:     Assume DOS date = 03/22/91
  28.                      Assume first file renamed on that date
  29.  
  30.                      ATBBS    becomes  ATB0322A.QWK
  31.                      SEMWARE  becomes  SEM0322A.QWK
  32.                      ABCDEFGH becomes  ABC0322A.QWK
  33.                      AB       becomes  AB0322A.QWK
  34.                      A        becomes  A0322A.QWK
  35.  
  36.     4. If no file name is provided, a message displays correct syntax.
  37.     5. If the file name is not found, a message is displayed saying so.
  38.  
  39. ======================================================================= */
  40.  
  41. #include <stdio.h>
  42. #include <io.h>
  43. #include <dos.h>
  44. #include <string.h>
  45. #include <process.h>
  46.  
  47. FILE *oldfile;
  48. FILE *newfile;
  49. struct date d;
  50.  
  51. char newname[13];
  52. char oldname[13];
  53. char prefix[4];
  54. int x;
  55.  
  56. /*-------------------------------------------------------------------- */
  57. int main(int argc, char *argv[])
  58.   {
  59.   if(argc < 2)                        /* No argument entered */
  60.     {
  61.     printf("Please enter a file name to rename ...\n");
  62.     printf("Syntax: QNAME2 <filename>\n\n");
  63.     exit(1);
  64.     }
  65.   else
  66.     {
  67.     strncat(oldname,argv[1],8);     /* Copy eight chars from argument  */
  68.     if(strlen(oldname) > 3)         /* If name of file is greater than */
  69.       {                             /*         three chars,            */
  70.       strncat(prefix,oldname,3);  /* Copy first three characters */
  71.       }
  72.     else                            /*            else             */
  73.       {
  74.       strcpy(prefix,oldname);     /* Copy entire string */
  75.       }
  76.     strcat(oldname,".QWK");         /* Add .QWK extension to filename */
  77.     strupr(oldname);                /* Convert name to upper case */
  78.     strupr(prefix);                 /* Convert prefix to upper case */
  79.     }
  80.  
  81.   /* Make sure file (oldname) exists */
  82.   if((oldfile = fopen(oldname,"rt")) == NULL)
  83.     {
  84.     printf("%s not found in current directory ...\n\n",oldname);
  85.     return 1;
  86.     }
  87.   else
  88.     /* The file exists and is open. Close it */
  89.    fclose(oldfile);
  90.  
  91.   /* Get today's date */
  92.   getdate(&d);
  93.  
  94.   /* x = letter of the alphabet.  Start at "A", ASCII 65 */
  95.   x = 65;
  96.  
  97.   /*  Use sprintf() to combine the month and day and the letter into
  98.       a file name.  Check to see if it doesn't exist and rename. Do
  99.       this until letter = "Z" */
  100.   do
  101.     {
  102.     sprintf(newname, "%s%02u%02u%c.QWK", prefix, d.da_mon, d.da_day, x);
  103.  
  104.     if((newfile = fopen(newname,"rt")) == NULL)
  105.       {
  106.         /* File name doesn't exist, rename original to unused file name */
  107.       if(rename(oldname,newname) != 0)
  108.         {
  109.         printf("Error renaming %s to %s\n\n",oldname,newname);
  110.         return 1;
  111.         }
  112.       else
  113.           /* If the rename was succesful, we're done with this loop */
  114.        break;
  115.       }
  116.     else
  117.       /* If the file existed, close it and go to next file name */
  118.      fclose(newfile);
  119.     }
  120.   while (++x <= 90);
  121.  
  122.   /* If more than 26 files exist for today's date, give error message */
  123.   if(x > 90)
  124.     {
  125.     printf("\nUnable to rename beyond %s%02u%02u%c.QWK\n",
  126.      prefix, d.da_mon, d.da_day, x - 1 );
  127.     return 1;
  128.     }
  129.  
  130.   /* Rename completed */
  131.   printf("%s renamed to %s\n\n",oldname,newname);
  132.   return 0;
  133.   }
  134.