Delete a directory(-tree)

[Autolink] Menu

 
/* ------------------------------------------------------------------ */
/* function: Delete all files in a directory and in all its           */
/*           sub directories!                                         */
/*                                                                    */
/* call:     DeleteDirectory dirToDelete                              */
/*                                                                    */
/* where:    dirToDelete - directory to delete                        */
/*                                                                    */
/* returns:  0 - okay                                                 */
/*          -1 - drive not ready                                      */
/*          -2 - missing or invalid parameter                   v3.10 */
/*                                                                    */
/* note:     see also The function SysDestroyObject                   */
/*                                                                    */
DeleteDirectory: PROCEDURE
  parse arg dirToDelete

  signal off error                                           /* v3.20 */
  signal off notready                                        /* v3.20 */
  signal off failure                                         /* v3.20 */

  if dirToDelete = '' then                                   /* v3.10 */
  do                                                         /* v3.10 */
                        /* check for missing parameter          v3.10 */
    thisRC = -2                                              /* v3.10 */
    signal DeleteDirectoryError                              /* v3.10 */
  end /* if */

                        /* file or device for messages                */
  prog.__LogAll = "2>NUL 1>NUL"

                        /* init the return code                       */
  thisRC = -1
                        /* check if the drive is ready                */
  SIGNAL ON NOTREADY Name DeleteDirectoryError
  call stream fileSpec( "drive", dirToDelete ) || "\*"

                        /* flush the REXX queue                 v3.20 */
  do while queued() <> 0; parse pull; end;

                        /* put a list of all subdirectories in the    */
                        /* queue                                      */
  "@dir /s/f /Ad " dirToDelete "2>NUL | RXQUEUE /lifo "

  do while queued() <> 0
    dirToDel = lineIn( "QUEUE:" )
    if dirTodel <> "" & right( dirToDel,2 ) <> "\." & ,
       right( dirToDel,3 )  <> "\.." then
    do
                    /* also delete hidden, system and read-only files */
                                                             /* v3.20 */
      '@attrib -r -s -h "' || dirToDel || '\*.*"' "2>NUL 1>NUL"

                                                             /* v3.20 */
      if stream( dirToDel || '\*.*', 'c', 'QUERY EXISTS' ) <> '' then
        '@del /n "' || dirToDel || '\*.*"' prog.__LogAll     /* v3.00 */

      if dirToDel <> dirToDelete then
        '@rd  "' || dirToDel || '"' prog.__LogAll            /* v3.00 */
    end /* if dirToDel <> "" then */
  end /* do while queued <> 0 */

                    /* also delete hidden, system and read-only files */
                                                             /* v3.20 */
  '@attrib -r -s -h "' || dirToDelete || '\*.*"' "2>NUL 1>NUL"

                                                             /* v3.20 */
  if stream( dirToDelete || '\*.*', 'c', 'QUERY EXISTS' ) <> '' then
    '@del /n "' || dirToDelete || '\*.*"' prog.__LogAll

  '@rd "' || dirToDelete || '"' prog.__logAll                /* v3.00 */

  thisRC = 0

DeleteDirectoryError:

RETURN thisRC


[Back: Create a directory(-tree)]
[Next: Check if a name describes a device or a file]