[comp.sys.ibm.pc] Deleting files QUICKLY

marks@tekigm2.TEK.COM (Mark D. Salzman) (07/23/87)

After many replys to my request, here is the one working solution that was
given on how to delete a large number of files QUICKLY from within a program.
That solution is to use the old DOS delete function 13H with wildcards to
specify the files you wish to delete. As usual it has it's tradeoffs. This
function does not understand path specifications at all. So it can only
delete files in the current directory. It also gets it's speed by using wild
cards since it only has to open the directory file once to do the deletions.

Much thanks goes to Ralf Brown (Ralf.Brown@b.gp.cs.cmu.edu) who showed me
that the DOS "del" command uses this function (along with some code to change
directories if a path is given) to do it's work.

Although many of you suggested going around DOS and mucking with the directory
file and File Allocation Table (FAT) directly, I have not yet received enough
information to do that safely. If anyone has such information, I would still
love to hear it.

Below is a program I wrote to test this function and determine how to use it.

Enjoy!

Mark D. Salzman     Phone (206) 253-5542.  |  The more complex the mind,
Tektronix Inc., P.O. Box 3500, M/S C1-937  |  the greater the need for 
Vancouver, Washington. 98668               |  the simplicity of play.
{world_at_large}!tektronix!tekigm2!marks   |       James T. Kirk
############################################################################
/*
 * DELETE.C
 *
 * This program implements the PC/MS-DOS delete function 13h. This is the
 * fastest function for deleting large numbers of files as long as the files
 * can be specified with wildcards ( *, ? ). As it stands, the program can
 * only delete files on the current drive and in the current directory, no
 * path names allowed. It will not delete files with special attributes like
 * readonly or system. This program is donated to the public domain.
 *
 * Compile with Microsoft C V4.0 :  cl delete.c -o delete
 *
 * By Mark Salzman, Tektronix Inc.
 */ 
#include <stdio.h>
#include <dos.h>

struct FCB	/* The DOS File Control Block */
{
    char exflag;        /* Extension active flag byte, FF = active */
    char space1[5];     /* Reserved space, normally set to zero */
    char attribute;     /* File attribute byte */
    char drivenum;      /* The drive number, also base address of FCB */
    char filenam[8];    /* File or device name, left justified space filled */
    char fileext[3];    /* File extension */
    int  block;         /* Current block number */
    int  record;        /* Record size */
    long size;          /* File size in bytes */
    int  date;          /* File date */
    char space2[10];    /* Reserved space for DOS control work */
    char crecord;       /* Current record (<127) */
    long rrecord;       /* Random record number */
}

main (argc, argv)   /* Set up to pass command line arguments */
int argc;
char *argv[];
{
    int inc;
    
    if (argc < 2) usage();	/* If no arguments, print usage. */
    for(inc=1; inc<argc; inc++)
        delete(argv[inc]);
}

int delete(fname)
char *fname;	/* File name and extension */
{
    union REGS ir, or;
    struct FCB tmp;
    int i;
    
    tmp.drivenum = 0;	/* use current drive */

    for(i=0; i<8; i++) tmp.filenam[i] = '\040';	/* Clear FCB name entries */
    for(i=0; i<3; i++) tmp.fileext[i] = '\040';

    /* Place file name (with wildcards) into FCB */
    i = 0;
    while((i<8) && (*fname != '\0') && (*fname != '.'))
    {
        tmp.filenam[i] = *fname;
        fname++; i++;
    }
    if(*fname == '.')
    {
        fname++;
        i = 0;
        while((i<3) && (*fname != '\0'))
        {
            tmp.fileext[i] = *fname;
            fname++; i++;
        }
    }
    
    /* Call DOS to remove the file(s) */
    ir.x.ax = 0x1300;		/* DOS delete function 13h */
    ir.x.dx = (unsigned)(&tmp.drivenum);	/* FCB base address */
    intdos(&ir, &or);		/* Delete all matching file names */

    return(or.x.ax);		/* Return errors if any */
}

/*
 * Print a program usage message, and exit.
 */
usage()
{
    printf("\nUSAGE: delete file_name(s)\n\n");
    exit(0);
}
##############################################################################
-- 
Mark D. Salzman     Phone (206) 253-5542.  |  The more complex the mind,
Tektronix Inc., P.O. Box 3500, M/S C1-937  |  the greater the need for 
Vancouver, Washington. 98668               |  the simplicity of play.
{world_at_large}!tektronix!tekigm2!marks   |       James T. Kirk