home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / c / delete.c < prev    next >
Internet Message Format  |  1994-03-04  |  4KB

  1. From: marks@tekigm2.TEK.COM (Mark D. Salzman)
  2. Newsgroups: comp.sys.ibm.pc
  3. Subject: Deleting files QUICKLY (a solution)
  4. Date: 23 Jul 87 16:16:25 GMT
  5. Keywords: DOS delete
  6.  
  7. After many replys to my request, here is the one working solution that
  8. was given on how to delete a large number of files QUICKLY from within
  9. a program.  That solution is to use the old DOS delete function 13H
  10. with wildcards to specify the files you wish to delete. As usual it
  11. has it's tradeoffs. This function does not understand path
  12. specifications at all. So it can only delete files in the current
  13. directory. It also gets it's speed by using wild cards since it only
  14. has to open the directory file once to do the deletions.
  15.  
  16. Much thanks goes to Ralf Brown (Ralf.Brown@b.gp.cs.cmu.edu) who showed
  17. me that the DOS "del" command uses this function (along with some code
  18. to change directories if a path is given) to do it's work.
  19.  
  20. Although many of you suggested going around DOS and mucking with the
  21. directory file and File Allocation Table (FAT) directly, I have not
  22. yet received enough information to do that safely. If anyone has such
  23. information, I would still love to hear it.
  24.  
  25. Below is a program I wrote to test this function and determine how to
  26. use it.
  27.  
  28. Enjoy!
  29.  
  30. Mark D. Salzman     Phone (206) 253-5542.  |  The more complex the mind,
  31. Tektronix Inc., P.O. Box 3500, M/S C1-937  |  the greater the need for 
  32. Vancouver, Washington. 98668               |  the simplicity of play.
  33. {world_at_large}!tektronix!tekigm2!marks   |       James T. Kirk
  34. ############################################################################
  35. /*
  36.  * DELETE.C
  37.  *
  38.  * This program implements the PC/MS-DOS delete function 13h. This is the
  39.  * fastest function for deleting large numbers of files as long as the files
  40.  * can be specified with wildcards ( *, ? ). As it stands, the program can
  41.  * only delete files on the current drive and in the current directory, no
  42.  * path names allowed. It will not delete files with special attributes like
  43.  * readonly or system. This program is donated to the public domain.
  44.  *
  45.  * Compile with Microsoft C V4.0 :  cl delete.c -o delete
  46.  *
  47.  * By Mark Salzman, Tektronix Inc.
  48.  */ 
  49. #include <stdio.h>
  50. #include <dos.h>
  51.  
  52. struct FCB    /* The DOS File Control Block */
  53. {
  54.     char exflag;        /* Extension active flag byte, FF = active */
  55.     char space1[5];     /* Reserved space, normally set to zero */
  56.     char attribute;     /* File attribute byte */
  57.     char drivenum;      /* The drive number, also base address of FCB */
  58.     char filenam[8];    /* File or device name, left justified space filled */
  59.     char fileext[3];    /* File extension */
  60.     int  block;         /* Current block number */
  61.     int  record;        /* Record size */
  62.     long size;          /* File size in bytes */
  63.     int  date;          /* File date */
  64.     char space2[10];    /* Reserved space for DOS control work */
  65.     char crecord;       /* Current record (<127) */
  66.     long rrecord;       /* Random record number */
  67. }
  68.  
  69. main (argc, argv)   /* Set up to pass command line arguments */
  70. int argc;
  71. char *argv[];
  72. {
  73.     int inc;
  74.     
  75.     if (argc < 2) usage();    /* If no arguments, print usage. */
  76.     for(inc=1; inc<argc; inc++)
  77.         delete(argv[inc]);
  78. }
  79.  
  80. int delete(fname)
  81. char *fname;    /* File name and extension */
  82. {
  83.     union REGS ir, or;
  84.     struct FCB tmp;
  85.     int i;
  86.     
  87.     tmp.drivenum = 0;    /* use current drive */
  88.  
  89.     for(i=0; i<8; i++) tmp.filenam[i] = '\040';    /* Clear FCB name entries */
  90.     for(i=0; i<3; i++) tmp.fileext[i] = '\040';
  91.  
  92.     /* Place file name (with wildcards) into FCB */
  93.     i = 0;
  94.     while((i<8) && (*fname != '\0') && (*fname != '.'))
  95.     {
  96.         tmp.filenam[i] = *fname;
  97.         fname++; i++;
  98.     }
  99.     if(*fname == '.')
  100.     {
  101.         fname++;
  102.         i = 0;
  103.         while((i<3) && (*fname != '\0'))
  104.         {
  105.             tmp.fileext[i] = *fname;
  106.             fname++; i++;
  107.         }
  108.     }
  109.     
  110.     /* Call DOS to remove the file(s) */
  111.     ir.x.ax = 0x1300;        /* DOS delete function 13h */
  112.     ir.x.dx = (unsigned)(&tmp.drivenum);    /* FCB base address */
  113.     intdos(&ir, &or);        /* Delete all matching file names */
  114.  
  115.     return(or.x.ax);        /* Return errors if any */
  116. }
  117.  
  118. /*
  119.  * Print a program usage message, and exit.
  120.  */
  121. usage()
  122. {
  123.     printf("\nUSAGE: delete file_name(s)\n\n");
  124.     exit(0);
  125. }
  126. ##############################################################################
  127.