home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Zodiac Super OZ
/
MEDIADEPOT.ISO
/
FILES
/
16
/
FREEDOS.ZIP
/
FD_A4PRE.ZIP
/
SOURCE
/
POWERC.ZIP
/
DELTREE.C
next >
Wrap
C/C++ Source or Header
|
1995-02-08
|
4KB
|
130 lines
#include <stdio.h>
#include <string.h>
#include <dos.h>
#include <direct.h>
#define STDSTRLN 256
#define BOOLEAN char
#define TRUE 1
#define FALSE 0
#define TESTMODE FALSE /* Test-Mode vs. Honest-to-God-Delete-Mode */
int wholedrive = FALSE; /* whether or not to delete entire drive */
/*
----------------------------------------------
*/
void zaploop(char *filepath) /* recursive erase / rmdir loop */
{
char filename[STDSTRLN + 1], searchpathname[STDSTRLN + 1], *chrptr;
struct ffblk flrec;
union REGS intregs;
struct SREGS intsregs;
BOOLEAN fndresult;
strcpy(searchpathname, filepath); /* compose file template to search on */
strcat(searchpathname, "\\*.*");
fndresult = findfirst(searchpathname, &flrec,
FA_DIREC + FA_RDONLY + FA_HIDDEN + FA_SYSTEM + FA_ARCH);
while (!fndresult) /* keep searching for files until there are no more */
{
if (flrec.ff_name[0] != '.') /* ignore "." and ".." directories */
{
strcpy(filename, filepath);
strcat(filename, "\\");
strcat(filename, flrec.ff_name);
if (flrec.ff_attrib & FA_DIREC) /* found directory: call recursive */
{
zaploop(filename);
if (TESTMODE) printf("%s\n", filename);
else rmdir(filename);
}
else /* found file: erase it */
{
chrptr = &filename;
intsregs.ds = FP_SEG(chrptr);
intregs.x.dx = FP_OFF(chrptr);
if (TESTMODE)
{
printf(" %s\n", filename);
}
else
{
if (flrec.ff_attrib & (FA_HIDDEN + FA_SYSTEM + FA_RDONLY))
{
intregs.h.ah = 0x43;
intregs.h.al = 0x01;
intregs.h.ch = 0x00;
intregs.h.cl = 0x00;
intdosx(&intregs, &intregs, &intsregs); /* set attr to erase */
}
remove(filename);
}
}
}
fndresult = findnext(&flrec); /* looking for next file */
}
}
/*
----------------------------------------------
*/
void setupzap(char *temp2zap) /* prepare for recursive erase / rmdir */
{
int ind;
char filepath[STDSTRLN + 1];
strcpy(filepath, temp2zap);
ind = strlen(filepath) - 1;
/* kill backslashes at end of path */
while ((ind >= 0) && (filepath[ind] == '\\')) filepath[ind--] = '\0';
/* verify that we're not trying to DELTREE a root directory without
the WHOLEDRIVE paramter */
if (((filepath[0] == '\0') || (filepath[ind] == ':')) && (!wholedrive))
printf("I won't try to eliminate an entire drive.\n");
else
{
zaploop(filepath); /* start recursive erase / rmdir */
if (!wholedrive) /* don't try to delete root itself! */
{
if (TESTMODE) printf("%s\n", filepath);
else rmdir(filepath);
}
}
}
/*
----------------------------------------------
*/
void main(int argc, char *argv[])
{
char cnfrm;
if (TESTMODE) printf("(Note: this program running in TEST MODE.)\n");
if (argc < 2)
{
printf("\nDELTREE\n\n");
printf("Purpose: to remove all files / subdirectories from the \n");
printf(" specified path\n\n");
printf("Usage: DELTREE path [WHOLEDRIVE]\n\n");
printf("Example: \"DELTREE C:\\UTIL\" will eliminate all files in\n");
printf(" C:\\UTIL, as well as clearing out/removing all\n");
printf(" subdirectories under and including C:\\UTIL. The\n");
printf(" WHOLEDRIVE option is needed to delete all files on\n");
printf(" a drive, and the path must consist of only the\n");
printf(" drive name (example: \"DELTREE C: WHOLEDRIVE\").\n\n");
}
else
{
strupr(argv[1]);
if (!strcmpi(argv[2], "WHOLEDRIVE")) wholedrive = TRUE;
printf("\nConfirm: eliminate %s? (y/n) ", argv[1]);
scanf("%c", &cnfrm);
printf("\n");
strupr(&cnfrm);
if (cnfrm == 'Y') setupzap(argv[1]);
}
}