midrange.com code scratchpad
Name:
Remove Empty Directories
Scriptlanguage:
Plain Text
Tabwidth:
4
Date:
10/05/2021 01:42:33 pm
IP:
Logged
Description:
An example using the UNIX-Type APIs, and recursion.

The problem of removing directories which have no files downstream has a naturally recursive definition.

If all of a directory's child directories can be deleted, then the directory can be deleted.
Code:
  1. An example using the UNIX-Type APIs, and recursion.
  2.  
  3. The problem of removing directories which either have no files downstream has a naturally recursive definition.
  4.  
  5. If all of a directory's child directories can be deleted, then the directory can be deleted.
  6.  
  7.  
  8.  
  9.       //--------------------------------------------------------------------------------------------------------------//
  10.       //                                                                                                              //
  11.       //                                                                                                              //
  12.       //                                   Remove Empty Directories                                                   //
  13.       //                                                                                                              //
  14.       //                                                                                                              //
  15.       //--------------------------------------------------------------------------------------------------------------//
  16.        Ctl-Opt dftActGrp(*No) actGrp(*Caller)
  17.                debug(*Yes) option(*SrcStmt:*NoDebugIO:*NoUnRef)
  18.                Main(main)                                                      ;
  19.       //--------------------------------------------------------------------------------------------------------------//
  20.       //                                                                                                              //
  21.       //... procedure interfaces ...                                                                                  //
  22.       //                                                                                                              //
  23.       //--------------------------------------------------------------------------------------------------------------//
  24.       //
  25.       // UNIX-Type APIs
  26.       //
  27.        Dcl-PR closeDir      Pointer         ExtProc('closedir')                ;
  28.          *n                 Pointer         Value                              ;
  29.        End-PR                                                                  ;
  30.        Dcl-PR lstat         Pointer         ExtProc('lstat')                   ;
  31.          *n                 Pointer         Value                              ;
  32.          *n                 Pointer         Value                              ;
  33.        End-PR                                                                  ;
  34.        Dcl-PR openDir       Pointer         ExtProc('opendir')                 ;
  35.          *n                 Pointer         Value                              ;
  36.        End-PR                                                                  ;
  37.        Dcl-PR readDir       Pointer         ExtProc('readdir')                 ;
  38.          *n                 Pointer         Value                              ;
  39.        End-PR                                                                  ;
  40.        Dcl-PR rmDir         Int(10)         ExtProc('rmdir')                   ;
  41.          *n                 Pointer         Value                              ;
  42.        End-PR                                                                  ;
  43.       //--------------------------------------------------------------------------------------------------------------//
  44.       //                                                                                                              //
  45.       // ... data structures ...                                                                                      //
  46.       //                                                                                                              //
  47.       //--------------------------------------------------------------------------------------------------------------//
  48.       *
  49.       * file status information
  50.       *
  51.        Dcl-DS fStat         Len(1024)       Qualified                          ;
  52.          mod                Int(10)         Pos(1)                             ;
  53.          dat                Int(10)         Pos(29)                            ;
  54.          siz                Int(10)         Pos(45)                            ;
  55.          typ                Char(10)        Pos(49)                            ;
  56.        End-DS                                                                  ;
  57.        Dcl-S  fStat@        Pointer         Inz(%Addr(fStat))                  ;
  58.       *
  59.       * directory entry information
  60.       *
  61.        Dcl-DS dirEnt        Len(696)        Based(dirEnt@)                     ;
  62.          szObj              Int(10)         Pos(53)                            ;
  63.          obj                Char(640)       Pos(57)                            ;
  64.        End-DS                                                                  ;
  65.       //--------------------------------------------------------------------------------------------------------------//
  66.       //                                                                                                              //
  67.       // ... global variables/constants ...                                                                           //
  68.       //                                                                                                              //
  69.       //--------------------------------------------------------------------------------------------------------------//
  70.        Dcl-C  #DIRECTORY    '*DIR'                                             ;
  71.        Dcl-C  #TRIMMER      x'4000'                                            ;
  72.       //--------------------------------------------------------------------------------------------------------------//
  73.       //                                                                                                              //
  74.       //                                                 Procedures                                                   //
  75.       //                                                                                                              //
  76.       //--------------------------------------------------------------------------------------------------------------//
  77.       //                                                  Mainline                                                    //
  78.       //--------------------------------------------------------------------------------------------------------------//
  79.        Dcl-Proc main                                                           ;
  80.        Dcl-PI   *n                                          ExtPgm             ;
  81.          pmPath             Char(80)                                           ;
  82.        End-PI                                                                  ;
  83.        Dcl-S  path          Char(256)                                          ;
  84.  
  85.        path = init( pmPath )                                                   ;
  86.        empty( path )                                                           ;
  87.  
  88.        Return                                                                  ;
  89.        End-Proc                                                                ;
  90.       //--------------------------------------------------------------------------------------------------------------//
  91.        Dcl-Proc empty                                                          ;
  92.        Dcl-PI   *n          Ind                                                ;
  93.          path               Char(256)       Value                              ;
  94.        End-PI                                                                  ;
  95.  
  96.        Dcl-S  dir@          Pointer                                            ;
  97.        Dcl-S  rtnVal        Ind             Inz(*On)                           ;// return on if dir deleted
  98.        Dcl-S  newPath       Char(256)                                          ;
  99.  
  100.        dir@ = openDir( %Addr(path) )                                           ;
  101.        If ( dir@ <> *Null )                                                    ;
  102.          dirEnt@ = readDir( dir@ )                                             ;// .
  103.          dirEnt@ = readDir( dir@ )                                             ;// ..
  104.  
  105.          dirEnt@ = readDir( dir@ )                                             ;
  106.          DoW ( dirEnt@ <> *Null )                                              ;
  107.  
  108.            lstat( %Addr(path) : fstat@ )                                       ;
  109.            If ( fStat.typ = #DIRECTORY )                                       ;
  110.              newPath = %SubSt(path:1:%Len(%TrimR(path:#TRIMMER))) +
  111.                        %SubSt(obj:1:szObj) + '/' + x'00'                       ;
  112.              If ( Not empty( newPath ) )                                       ;
  113.                rtnVal = *Off                                                   ;
  114.              EndIf                                                             ;
  115.            Else                                                                ;
  116.              rtnVal = *Off                                                     ;// contains files
  117.            EndIf                                                               ;
  118.  
  119.            dirEnt@ = readDir( dir@ )                                           ;
  120.          EndDo                                                                 ;
  121.          If ( rtnVal )                                                         ;
  122.            rmDir( %Addr(path) )                                                ;
  123.          EndIf                                                                 ;
  124.  
  125.        EndIf                                                                   ;
  126.        closeDir( dir@ )                                                        ;
  127.        Return rtnVal                                                           ;
  128.        End-Proc                                                                ;
  129.       //--------------------------------------------------------------------------------------------------------------//
  130.        Dcl-Proc init                                                           ;
  131.        Dcl-PI   *n          Char(256)                                          ;
  132.          path               Char(80)                                           ;
  133.        End-PI                                                                  ;
  134.  
  135.        Return %TrimR(path:#TRIMMER) + x'00'                                    ;// assumes trailing slash
  136.  
  137.        End-Proc                                                                ;
  138.  
© 2004-2019 by midrange.com generated in 0.007s valid xhtml & css