home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 035 / cenvi29.zip / DELTREE.CMD < prev    next >
OS/2 REXX Batch file  |  1994-09-02  |  5KB  |  117 lines

  1. @echo off
  2. REM *******************************************************************
  3. REM *** DelTree - Delete a subdirectory and all files in it, and    ***
  4. REM *** ver.2     recursively delete all subdirectories within that ***
  5. REM ***           subdirectory.  You may look at DelTree1.cmd and   ***
  6. REM ***           DelTree2.cmd for other methods of doing the       ***
  7. REM ***           same thing by using temporary files.              ***
  8. REM *******************************************************************
  9.    if "%1"=="" GOTO SHOW_HOW
  10.  
  11. REM *******************************************************************
  12. REM *** The user could be very unhappy to delete lots of files they ***
  13. REM *** didn't really want to delete, and so give them a chance to  ***
  14. REM *** change their mind.                                          ***
  15. REM *******************************************************************
  16.    CEnvi return( stricmp(`NOASK`,`%2`) ? 0 : 1 )
  17.    IF ERRORLEVEL 1 GOTO ANSWER_YES
  18.    ECHO THIS WILL DELETE ALL FILES AND DIRECTORIES UNDER %1
  19.    CALL GetUKey.cmd "ARE YOU SURE YOU WANT TO DO THIS DELETE ALL THOSE INNOCENT FILES? (Y/N)"    yn
  20.    if N==%UKEY% GOTO FINI
  21.    :ANSWER_YES
  22.  
  23. REM *********************************************************************
  24. REM *** Check that this is a valid subdirectory.  This can be handled ***
  25. REM *** by calling another CEnvi program: ValidDir.cmd                ***
  26. REM *********************************************************************
  27.    CEnvi ValidDir.cmd %1 COMPLAIN
  28.    if ERRORLEVEL 1 GOTO FINI
  29.  
  30. REM ******************************************************************
  31. REM *** It will be simpler to delete files if we make sure that no ***
  32. REM *** attributes are set that would make them undeletable.  The  ***
  33. REM *** easy way to change this is to let the DOS attrib function  ***
  34. REM *** clear all of these attributes for us.                      ***
  35. REM ******************************************************************
  36.    attrib -H -S -R %1\*.* /s > NUL
  37.  
  38. REM **************************************************************************
  39. REM *** Temporarily turn off DELDIR to make this deletion fast (if unsafe) ***
  40. REM **************************************************************************
  41.    SETLOCAL
  42.    set DELDIR=
  43.  
  44. REM ******************************************************************
  45. REM *** The rest of this task will be handled by CEnvi, which will ***
  46. REM *** create a list of all directories and then call the command ***
  47. REM *** shell to delete the files in each directory.               ***
  48. REM ******************************************************************
  49. CEnvi %0.cmd %1
  50. GOTO CENVI_EXIT
  51.  
  52.    main(argc,argv)
  53.    {
  54.       // build a sorted list of all subdirectories under the specified directory
  55.       DirCount = BuildSubdirList(argv[1],DirList)
  56.       if ( DirCount != 0 ) {
  57.          // sort the subdir list in reverse alphabetic order, which insures that
  58.          // subdirectoies of a directory appear before its parent directory
  59.          qsort(DirList,DirCount,"ReverseSortDirnames")
  60.          // for each subdirectory, call command shell to delete all files in it
  61.          // and then remove the directory
  62.          for ( i = 0; i < DirCount; i++ ) {
  63.             DeleteAllFilesInDir( DirList[i].name );
  64.             RemoveDirectory( DirList[i].name );
  65.          }
  66.       }
  67.       // all subdirectories have have been deleted, then delete the input subdir
  68.       DeleteAllFilesInDir( argv[1] );
  69.       RemoveDirectory( argv[1] );
  70.    }
  71.  
  72.    BuildSubdirList(BaseDir,List) // build List of all subdirectories under BaseDir
  73.    {                             // return count of elements in list
  74.       sprintf(SearchSpec,"%s\\*",BaseDir)
  75.       List = Directory(SearchSpec,TRUE,
  76.                        FATTR_RDONLY|FATTR_HIDDEN|FATTR_SYSTEM|FATTR_SUBDIR|FATTR_ARCHIVE,
  77.                        FATTR_SUBDIR)
  78.       return( ( List == NULL ) ? 0 : 1+GetArraySpan(List) );
  79.    }
  80.  
  81.    ReverseSortDirnames(Dir1,Dir2) // repeatedly called by qsort to sort Dir1 and Dir2
  82.    {
  83.       return( stricmp(Dir2.name,Dir1.name) );
  84.    }
  85.  
  86.    DeleteAllFilesInDir(DirName) // call system to delete all files in this dir
  87.    {
  88.       printf("DEL %s\n",DirName)
  89.       system("echo Y | del %s > NUL",DirName)
  90.    }
  91.  
  92.    RemoveDirectory(DirName) // call system RMDIR call
  93.    {
  94.       printf("RMDIR %s\n",DirName)
  95.       system("RMDIR %s > NUL",DirName)
  96.    }
  97.  
  98. :CENVI_EXIT
  99. REM **********************
  100. REM *** Restore DELDIR ***
  101. REM **********************
  102.    ENDLOCAL
  103.    GOTO FINI
  104.  
  105. :SHOW_HOW
  106.    ECHO .
  107.    ECHO DelTree.cmd - Delete a subdirectory and all files and directories under it
  108.    ECHO .
  109.    ECHO USAGE: DelTree DirSpec [NOASK]
  110.    ECHO WHERE: NOASK: (optional) to not prompt "are you sure?"
  111.    ECHO        DirSpec: Specification for directory to delete
  112.    ECHO .
  113.    GOTO FINI
  114.  
  115. :FINI
  116.    SET UKEY=
  117.