home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / fw100a.zip / DIRCOMP.C < prev    next >
Text File  |  1995-11-13  |  9KB  |  251 lines

  1. /******************************************************************************
  2.     Directory Compare
  3.  
  4.     This program illustrates running command files to produce listings
  5.     which are then opened and compared...
  6.     The author offes No Guarantee as to the functionality of this program,
  7.     you can use this file as you see fit. If you destroy anything by using 
  8.     it you have only yourself to blame.
  9.     compiled using:-  "icc /ss dircomp.c"
  10.  
  11.     This_area_is_copyright_and_must_not_be_removed_from_an_unmodified_file(c)
  12.         {
  13.             Created by Peter Koller 1995. This Program is FREEWARE
  14.             All Rights Reserved.
  15.         }
  16.  
  17. example usage:-
  18.  
  19. a)  dircmp c:\apath
  20.     compares this directory with apath directory eg c:\apath\* with .\*
  21.  
  22. b)  dircmp /f listing               compare listing with .\*
  23.     dircmp c:\apath c:\bpath        compare c:\apath\* with c:\bpath\*
  24.  
  25. c)  dircomp /f listing c:\apath
  26.     dircomp c:\apath /f listing     compare listing with c:\apath\*
  27.  
  28.     create a listing using....> dir /b /o:n c:\apath >listing
  29. ******************************************************************************/
  30. #define     TRUE    1
  31. #define     FALSE   0
  32. #include    <string.h>
  33. #include    <stdio.h>
  34. #include    <stdlib.h>
  35.  
  36. typedef unsigned long BOOL;
  37.  
  38. void    main(int argc,char *argv[]);
  39.  
  40. int     UseFile = 0;
  41. int     fcount = 0,
  42.         bcount = 0;
  43. BOOL    flag = FALSE;
  44. BOOL    IsEOF = FALSE;
  45. char    *FilePathA;
  46. char    *FilePathB;
  47. char    Here[] = ".";
  48.  
  49. char    Envir[512];
  50. char    Batch[512];
  51. char    AFile[512];
  52. char    BFile[512];
  53. char    Temp[512];
  54. char    *FileToOpen;
  55. FILE    *one;
  56. FILE    *two;
  57.  
  58. /**** Main Program Entry ****/
  59. void    main(int argc,char *argv[])
  60.     {
  61.         int     x;
  62.  
  63.  
  64.         /**** Get the TMP Envrionment path ****/
  65.         strcpy(Envir,getenv("TMP"));
  66.         if(strcmp(Envir,"") == 0)
  67.             {
  68.                 printf("Error - Missing TMP environment setting");
  69.                 goto done;
  70.             }
  71.  
  72.         /**** Parse the command line ****/
  73.         for(x = 1; x < argc; x++)
  74.             {
  75.                 if ( strnicmp("/f", argv[x], 2) == 0 ) UseFile = x;
  76.             }
  77.         switch(argc)
  78.             {
  79.                 case 1:
  80.                     printf("Error - Too few arguments \n");
  81.                     goto done;
  82.                     break;
  83.                 case 2:
  84.                     if(UseFile)
  85.                         {
  86.                             printf("Error - Too few arguments \n");
  87.                             goto done;
  88.                         }
  89.                     else
  90.                         {
  91.                             FilePathA = argv[1];
  92.                             FilePathB = Here;
  93.                         }
  94.                     break;
  95.                 case 3:
  96.                     switch(UseFile)
  97.                         {
  98.                             case 0:
  99.                                 FilePathA = argv[1];
  100.                                 FilePathB = argv[2];
  101.                                 break;
  102.                             case 1:
  103.                                 FilePathA = argv[2];
  104.                                 FilePathB = Here;
  105.                                 break;
  106.                             case 2:
  107.                                 printf("Error - Inconsistent argument order \n");
  108.                                 goto done;
  109.                                 break;
  110.                         }
  111.                     break;
  112.                 case 4:
  113.                     switch(UseFile)
  114.                         {
  115.                             case 0:
  116.                                 printf("Error - Too many arguments \n");
  117.                                 goto done;
  118.                                 break;
  119.                             case 1:
  120.                                 FilePathA = argv[UseFile + 1];
  121.                                 FilePathB = argv[UseFile + 2];
  122.                                 break;
  123.                             case 2:
  124.                                 FilePathA = argv[UseFile + 1];
  125.                                 FilePathB = argv[1];
  126.                                 break;
  127.                             case 3:
  128.                                 printf("Error - Inconsistent argument order \n");
  129.                                 goto done;
  130.                                 break;
  131.                         }
  132.                 default:
  133.                     printf("Error - Too many arguments \n");
  134.                     goto done;
  135.                     break;
  136.             }
  137.  
  138.         /**** Create a command file to DIR the directories ****/
  139.         sprintf(Batch,"%s\\%s",Envir,"DIRC~~~~.CMD");
  140.         if(UseFile)
  141.             {
  142.                 sprintf(AFile,"%s",FilePathA); //a listing file
  143.  
  144.                 /**** write batch file ****/
  145.                 one = fopen(Batch,"w");
  146.                 fprintf(one, "@dir /b /o:n %s >%s\\DC~~CMP.DC~\n", FilePathB, Envir);
  147.                 fclose(one);
  148.  
  149.                 /**** run batch file ****/
  150.                 system(Batch);
  151.  
  152.                 /**** create tidyup batch file ****/
  153.                 one = fopen(Batch,"w");
  154.                 fprintf(one, "@del %s\\DC~~CMP.DC~\n",Envir);
  155.                 fclose(one);
  156.             }
  157.         else
  158.             {
  159.                 sprintf(AFile,"%s\\DD~~CMP.DC~",Envir); //a temp file
  160.  
  161.                 /**** write batch file ****/
  162.                 one = fopen(Batch,"w");
  163.                 fprintf(one, "@dir /b /o:n %s >%s\\DD~~CMP.DC~\n", FilePathA, Envir);
  164.                 fprintf(one, "@dir /b /o:n %s >%s\\DC~~CMP.DC~\n", FilePathB, Envir);
  165.                 fclose(one);
  166.  
  167.                 /**** run batch file ****/
  168.                 system(Batch);
  169.  
  170.                 /**** create tidyup batch file ****/
  171.                 one = fopen(Batch,"w");
  172.                 fprintf(one, "@del %s\\DD~~CMP.DC~\n", Envir);
  173.                 fprintf(one, "@del %s\\DC~~CMP.DC~\n", Envir);
  174.                 fclose(one);
  175.             }
  176.  
  177.         sprintf(BFile, "%s\\DC~~CMP.DC~", Envir); //a temp file
  178.  
  179.         /**** I now have two temporary files to compare... ****/
  180.         printf("Finding files in  %s missing in %s \n", FilePathA, FilePathB);
  181.  
  182.         one = fopen(AFile,"r");
  183.         two = fopen(BFile,"r");
  184.         *(AFile) = '\0';
  185.         *(BFile) = '\0';
  186.  
  187.         /**** First compare one with two... ****/
  188.         while(!IsEOF)
  189.             {
  190.                 /**** Get Filename in one ****/
  191.                 fgets(AFile,511,one);               //get a filename
  192.                 IsEOF = (BOOL)(feof(one) != 0);     // EOF ?
  193.  
  194.                 /**** Find Filename in two ****/
  195.                 flag = FALSE;                       //default file not found
  196.                 rewind(two);                        //goto start of two
  197.                 while(!flag && (feof(two) == 0) && !IsEOF)
  198.                     {
  199.                         fgets(BFile,511,two);
  200.                         if(strcmpi(AFile,BFile) == 0)flag = TRUE;
  201.                     }
  202.  
  203.                 /**** Not found so report it ****/
  204.                 if(!flag && !IsEOF)
  205.                     {
  206.                         fcount++;
  207.                         *(AFile + (strlen(AFile) - 1)) = '\0';
  208.                         printf("Forward:%d = %s\\%s missing in %s \n",
  209.                                 fcount, FilePathA, AFile, FilePathB);
  210.                     }
  211.             }
  212.  
  213.         rewind(two);
  214.         IsEOF = FALSE;
  215.  
  216.         /**** Then compare two with one... ****/
  217.         while(!IsEOF)
  218.             {
  219.                 /**** Get Filename in two ****/
  220.                 fgets(BFile,511,two);               //get a filename
  221.                 IsEOF = (BOOL)(feof(two) != 0);     // EOF ?
  222.  
  223.                 /**** Find Filename in one ****/
  224.                 flag = FALSE;                       //default file not found
  225.                 rewind(one);                        //goto start of one
  226.                 while(!flag && (feof(one) == 0) && !IsEOF)
  227.                     {
  228.                         fgets(AFile,511,one);
  229.                         if(strcmpi(AFile,BFile) == 0)flag = TRUE;
  230.                     }
  231.  
  232.                 /**** Not found so report it ****/
  233.                 if(!flag && !IsEOF)
  234.                     {
  235.                         fcount++;
  236.                         *(BFile + (strlen(BFile) - 1)) = '\0';
  237.                         printf("Backward:%d = %s\\%s missing in %s \n",
  238.                                 fcount, FilePathB, BFile, FilePathA);
  239.                     }
  240.             }
  241.  
  242.         /**** all done ****/
  243.         fclose(one);
  244.         fclose(two);
  245.         system(Batch);                  //delete temp files
  246.         sprintf(Temp,"del %s",Batch);   //delete batch file
  247.         system(Temp);
  248.         done:
  249.             return;
  250.     }
  251.