'===========================================================================
' Subject: Remove Directories from Floppy     Date: 10-19-03 (  :  )       
'  Author: Tony Damigo                        Code: PB                     
'  Origin: tldamigo@ccis.com                Packet: DISK.ABC
'===========================================================================
'===========================================================================
' Subject: DRIVE/DIRECTORY CLEANER            Date: 07-21-03 (10:58)
'  Author: Tony Damigo                        Code: PB
'  Origin: tldamigo@ccis.com                Packet: PB.ABC
'
'  Released to Public Domain
'  Created using PowerBasic Ver 3.5
'===========================================================================

'
' I'm not very good at writing code, so please forgive my bad
' habbits.
'
' I do a lot of floppy work and hate trying to delete my disks
' one directory at a time. With that in mind, I created this little
' program to assist me.
'
' You are free to use it as you wish - but please give credit where due.
'
' ************************ [ WARNING ] **************************
'
' !! DO NOT TEST ON YOUR HARD DRIVE UNLESS YOU WANT IT DELETED !!
'
' ***************************************************************
'
' Note - Your program is responsible for including a back-slash
' the in the path. Failure to do so may cause an endless loop.
'
' Example: Path$="D:\TEST\STANDARDS\"
'
' If path includes a list of directories - only the last directory,
' right end of path string, and all its branches will be deleted.
'
' Example: Path$="D:\A1\B1\C1\" <-- cleaning will start at C1\
'          and progress down the branch to D1\E1\F1\ etc.,
'          much like the Windows(tm) program deletes directories
'
' Try changing the directory path to see how it works. But I suggest
' you practice on a floppy disk drive. Not your dard drive.
'
' As emplimented here, no wildcards are allowed.
'
' NO ERROR CHECKING is done regarding non-valid drives or
' directorins. I leave that to your code :-)
'-------------------------------------------------------------


CLEAR
CLS
DEFINT A-Z
SHARED Drive%, Orgbb&&

RESET.CLEAN:

Drive%  =0
Orgbb&& =0

PATH$=Command$
IF PATH$="" THEN PATH$="A:\"   '--- Assums A: Drive to be on the safe side.
IF LEN(PATH$)=1 THEN PATH$=PATH$+":\"
IF LEN(PATH$)=2 THEN PATH$=PATH$+"\"


ORGPATH$=PATH$
'----------- Save Original Path

Drive%=Instr("ABCDEFGHIJKLMNOPQRSTUVWXYZ",LEFT$(ORGPATH$,1))
'----------- Assign number value to drive letter

RETEST:

IF DRIVESPACE&&(drive%) = 0 THEN
   LOCATE 3,1
   PRINT "No Disk in Drive ..."
   PRINT "Incert disk and hit any kay to continue"
   PRINT "Press ESC to end task now!"
   BEEP
   Action=Getkey%
   IF Action= 27 THEN
      CLS
      END
   END IF
   LOCATE 3,1
   PRINT STRING$(40," ")
   PRINT STRING$(40," ")
   PRINT STRING$(40," ")
   GOTO RESET.CLEAN
END IF

' ----------

Orgbb&&=FREESPACE&&(drive%)
'----------- Remember Original value of free space

CALL PRINTSpecs
' ---------- Print Drive Info on screen



' -------------------[ Start the main loop ]---------------------

  Temp$=Path.2.Delete$( PATH$ )

  DO WHILE Temp$<>OrgPath$
     CALL PRINTSpecs
     IF Del.Directory ( Temp$ )=0 THEN
       Temp$=Path.2.Delete$( PATH$ )
     END IF
  LOOP

  Del.Directory ( Temp$ )
  temp$=Path.2.Delete$( Path$ )
  Del.Directory ( Temp$ )
  CALL PRINTSpecs
  BEEP

  LOCATE 13,23 :  PRINT "Please Incert Another Floppy Disk"
  LOCATE 14,20 :  PRINT "Press any key to continue, or Esc to End"

 DO
   Action=Getkey%
   IF Action=27 THEN
      CLS
      END
   ELSE

      LOCATE 13,23 :  PRINT STRING$(45," ")
      LOCATE 14,20 :  PRINT STRING$(45," ")
   END IF

   GOTO RESET.CLEAN:
LOOP
END

'---------------------------------------------------------------


FUNCTION Del.Directory ( Fmask$ )
'---------------------------------------------------------------
' Deletes all files in the directory indicated in Fmask$
' - also looks for read only files and changes to normal
' If no files are present it deletes the directior
'---------------------------------------------------------------
ON LOCAL ERROR GOTO DDTRAP
MASK$=FMASK$
 IF DirIsEmpty (Mask$)=0 THEN

    RMDIR RTRIM$(Mask$,"\")
    CALL PRINTSpecs
 ELSE
    File$=DIR$(Mask$+"*.*",16)
    WHILE File$<>""
      X= ATTRIB( Mask$+File$ )
      IF X <> 16 THEN
         IF X <> 0 THEN
            ATTRIB Mask$+File$, 0
            KILL Mask$+File$
         END IF
      END IF
      File$=DIR$

      CALL PRINTSpecs

    WEND
 END IF
   FUNCTION=1

 EXIT FUNCTION
 DDTRAP:
   RESUME DDEXIT
 DDEXIT:
   FUNCTION=0
END FUNCTION

FUNCTION Path.2.Delete$( FMask$ )
'---------------------------------------------------------------
' FMask$ is the starting point for the File Path. It will search
' a single path to it's end, but it will only fallow 1 brach of
' that path.
'---------------------------------------------------------------
  Result$=""
  Org$=FMask$
  Mask$=FMask$
  CheckNext:

  File$ = DIR$ ( Mask$+"*",16 )

  DO WHILE FILE$ <> ""
    Result$ = Result$+File$+"\"
    File$   = Dir$
    Mask$=Org$+Result$
    GOTO CheckNext ' ----------------- ' re-start search until all
  LOOP                                 ' dir's are found along path
  FUNCTION=Mask$                       ' then exit loop
  EXIT FUNCTION

END FUNCTION


FUNCTION DirIsEmpty( FMask$ )
' --------------------------------------------
' Duh! Checks to see if directory is empty
' --------------------------------------------
  IF dir$(FMask$+"*.*",6) > "" THEN
     FUNCTION=1
  ELSE
     FUNCTION=0
  END IF
END FUNCTION

FUNCTION FREESPACE&& ( Drive%)   '-------- Find Free Space
 REG 4,Drive%
 REG 1,&H3600
 CALL INTERRUPT &H21
 FUNCTION= CQUD(REG(2)) * REG(3) * REG(1)
END FUNCTION


FUNCTION DRIVESPACE&& ( Drive% ) '-------- Get Disk Capacity
 REG 4,Drive%
 REG 1,&H1C00
 CALL INTERRUPT &H21
 FUNCTION= CQUD(REG(4)) * REG(3) * REG(1)
END FUNCTION

Sub PRINTSpecs
 SHARED Drive%,Orgbb&&,Reclaimed&&
  aa&&=DRIVESPACE&& ( Drive% )
  bb&&=FREESPACE&&( Drive% )

  IF Reclaimed&& =0 THEN Reclaimed&&=bb&&
  LOCATE 1,2 : PRINT "Clean Disk Ver 01"
  LOCATE 4,2 : PRINT USING "Total Disk Capacity   : ############# ";aa&&
  LOCATE 5,2 : PRINT USING "Available Free Space  : ############# ";bb&&
  LOCATE 6,2 : PRINT USING "Reclaimed File Space  : ############# ";(bb&&-Reclaimed&&)

'----------------------------------------------------------------
' This is used to display info during full delete

  SVal& = (clng(bb&&) / clng(aa&&)) * 99.5
  LOCATE 7,2 : PRINT USING "Task ###";SVal&;
  PRINT "% Compleated"
' ------------------------------------------------
End Sub

FUNCTION GETKEY%
' semple key input

  ky$=""
  DO WHILE ky$=""
     ky$=UCASE$(inkey$)
  LOOP
  FUNCTION=ASCII(ky$)
END FUNCTION



