'===========================================================================
' Subject: PRUNE FILES AND DIRECTORY          Date: Unknown Date           
'  Author: Dave Navarro, Jr.                  Code: PB32                   
'  Origin: comp.lang.basic.misc             Packet: DOS.ABC
'===========================================================================
' Prune all files in a directory tree and remove all directories.
' Released to the Public Domain by Dave Navarro, Jr.
' Requires PowerBASIC 3.2 or later.

$STACK 4096           'uses recursion, so use a large stack
$INCLUDE "PB32.INC"

DEFINT A-Z

Direc$ = UCASE$(COMMAND$)

ErCode = Prune(Direc$)

SELECT CASE ErCode
  CASE 1 : PRINT Direc$; " not found!"
  CASE 2 : PRINT Direc$; " is not a directory!"
END SELECT

END ErCode

FUNCTION Prune(BYVAL directory AS STRING) PUBLIC AS INTEGER

  IF NOT Exist(directory) THEN
    FUNCTION = 1                 'directory not found
    EXIT FUNCTION
  END IF

  IF (ATTRIB(directory) AND 16) <> 16 THEN
    FUNCTION = 2                 'not a directory
    EXIT FUNCTION
  END IF

  KillFiles directory

  RMDIR directory

END FUNCTION

SUB KillFiles(directory AS STRING) PRIVATE

  DIM f            AS LOCAL STRING
  DIM DtaSeg       AS LOCAL INTEGER
  DIM DtaOFs       AS LOCAL INTEGER
  DIM OldDtaBuffer AS LOCAL STRING

  GetDTA DtaSeg, DtaOfs

  DEF SEG = DtaSeg
    OldDtaBuffer = PEEK$(DtaOfs, 44)     'save current DTA information
  DEF SEG

  directory = RTRIM$(directory, "\")
  PRINT directory + "\"

  f = DIR$(Directory + "\*.*",16)
  WHILE LEN(f)
    PRINT directory + "\" + f
    IF ASCII(f) <> 46 THEN
      IF (DtaAttrib AND 16) = 16 THEN
        KillFiles directory + "\" + f
        RMDIR directory + "\" + f
      ELSE
        KILL directory + "\" + f
      END IF
    END IF
    f = DIR$
  WEND

  DEF SEG = DtaSeg
    POKE$ DtaOfs, OldDtaBuffer           'restore saved DTA information
  DEF SEG

END SUB
