home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 328_01 / dir2bat.c < prev    next >
Text File  |  1990-09-06  |  5KB  |  161 lines

  1. char *help_string =
  2.  "  DIR2BAT  \n\n"
  3.  " takes DOS DIR output as its input (STDIN - amy be redirection)      \n"
  4.  " and creates a .BAT file as output (STDOUT)                        \n\n"
  5.  " dir  file listings have this form: (note collumn count)             \n"
  6.  "   01234567890123456789                                              \n"
  7.  "   FILENAME EXT                                                      \n"
  8.  "   DIRNAME      <DIR>                                              \n\n"
  9.  " the output will be                                                  \n"
  10.  "   user_stringFILENAME.EXTuser_string                              \n\n"
  11.  " The user_strings are set on the command line                        \n"
  12.  "  with the @ symbol to indicate where to put the file name         \n\n"
  13.  "  example:                                                           \n"
  14.  "       dir | dir2bat copy @ a: > newbat.bat                        \n\n"
  15.  "  the file newbat will have each line read:                          \n"
  16.  "    copy filename.ext a:                                         \n\n"
  17.  "  a useful way to call this program is:  DIR2BAT %1 %2 @ >newbat.bat \n"
  18.  "      then use NEWBAT CALL otherbat                                  \n"
  19.  "      and DOS will execute otherbat once for each file             \n\n"
  20.  "  to place the symbols    |    <    or  >  in the ouptut listing,    \n"
  21.  "  use ^ followed by       \\    ,        .   (lowercase, same key)   \n"
  22.  "  example:   dir | dir2bat type @ ^. contents.txt > newbat.bat       \n"
  23.  "             The lines in the output file (newbat.bat) will then read:\n"
  24.  "             type filename.ext > contents.txt                      \n\n"
  25.  " @copyright D BLUM 1989                                              \n";
  26.  
  27.  
  28. /* to compile this program under TurboC (produces dir2bat.com): 
  29.  *        tcc  -mt -lt -N-  dir2bat.c
  30.  */
  31.  
  32.  
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36.  
  37. main (int argc, char **argv) {
  38.  
  39. int n = 0;
  40. char inline[256];
  41. char outline[81];
  42. char *out_start;
  43. char *outptr;
  44. char second_string[256];
  45. char *ptr;
  46.  
  47. outline[0] = 0x00;
  48. out_start = outline;
  49.  
  50. /* get parameters and cocnatenate them to make a feeder */
  51. while ( ++n < argc)
  52.     {
  53.     strcat (outline, argv[n] );
  54.     strcat (outline, " ");
  55.     }
  56.  
  57. /* replace "^." with "> ",  "^," with "< " and  "^\\" with "| "
  58.  */
  59. outptr = outline;
  60. while ( (outptr = strchr(outptr, '^')) != NULL)
  61.     {
  62.     switch( *(outptr+1) )
  63.     {
  64.     case (','):
  65.         *outptr     = '<';
  66.         *(outptr+1)     = ' ';
  67.         break;
  68.     case ('.'):
  69.         *outptr     = '>';
  70.         *(outptr+1)     = ' ';
  71.         break;
  72.     case ('\\'):
  73.         *outptr     = '|';
  74.         *(outptr+1)     = ' ';
  75.         break;
  76.     default:
  77.         /* user actually wants ^ without replacement in output
  78.          * so must skip past it, otherwise would get infinite loop
  79.          */
  80.         ++outptr;
  81.     }    /*end switch */
  82.     }    /* end while */
  83.  
  84.  
  85.  
  86. /*look for the @ */
  87. out_start = strchr(outline, '@');
  88. if (out_start == NULL)
  89.     {
  90.     perror("ERROR - program DIR2BAT requires a @ on the command line\n\n\n");
  91.     perror(help_string);
  92.     exit(99);
  93.     }
  94.  
  95. /* move the text that follws the @ to a save area
  96. */
  97. strcpy(second_string, out_start+1);
  98.  
  99.  
  100. /*read the directory listing
  101. */
  102. while (gets(inline) != NULL)
  103.     {
  104.  
  105.     memset (out_start,' ', 13);
  106.  
  107.     if ( strchr (inline, '-')  && !strstr (inline, "<DIR>"  ))
  108.         {
  109.         /* if a dash is in the line (tru for files & dirs )
  110.                  *  and if it's not a DIR
  111.          *  then it is a file, not a directory
  112.          */
  113.  
  114.  
  115.         /* move the filename and the blank that follows */
  116.         memcpy(out_start, inline, 9);
  117.  
  118.         /* setup the period to separate filename from extension */
  119.         outptr = strchr ( out_start , ' ');
  120.         if (outptr == NULL)
  121.             {
  122.             outptr = out_start + 8;
  123.             }
  124.         *outptr  = '.';
  125.         ++outptr;
  126.  
  127.         /* copy the extension */
  128.         memcpy ( outptr, inline+9, 3);
  129.         /*move the second string in after the filename */
  130.                 ptr = strchr (outptr, ' ');
  131.                 if (ptr == NULL)
  132.                    {
  133.                    ptr = outptr + 3;
  134.                    }
  135.         strcpy ( ptr, second_string);
  136.         puts (outline);
  137.         }  /*end of file processing */
  138.  
  139.     else if ( strstr (inline, "<DIR>") )
  140.         {
  141.         /* this line lists a directory
  142.          */
  143.         memcpy ( out_start, inline, 8 );
  144.         outptr = strchr (out_start, ' ');
  145.         if (outptr == NULL)
  146.             {
  147.             outptr = out_start + 8;
  148.             }
  149.         /* mark this as a directory */
  150.         *outptr = '\\';
  151.         strcpy (outptr+1, second_string);
  152.         puts (outline);
  153.         } /*end of directory processing */
  154.  
  155.  
  156.  
  157.     } /*end of while !feof()*/
  158.  
  159.  
  160. return (0);
  161. } /*end of main */