home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / DELTREE.CMD < prev    next >
OS/2 REXX Batch file  |  1995-03-28  |  6KB  |  140 lines

  1. @echo off
  2. REM *******************************************************************
  3. REM *** DelTree - Delete a subdirectory and all files in it, and    ***
  4. REM *** ver.3     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 A%1Z==AZ 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.    CEnvi2 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 CEnvi2 program: ValidDir.cmd                ***
  26. REM *********************************************************************
  27.    CEnvi2 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 CEnvi2 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. CEnvi2 %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.             // some directories have spaces so need quotes
  64.             QuoteIfNeeded( DirList[i].name );
  65.             DeleteAllFilesInDir( DirList[i].name );
  66.             RemoveDirectory( DirList[i].name );
  67.          }
  68.       }
  69.       // all subdirectories have have been deleted, then delete the input subdir
  70.       QuoteIfNeeded( argv[1] );
  71.       DeleteAllFilesInDir( argv[1] );
  72.       RemoveDirectory( argv[1] );
  73.    }
  74.  
  75.    BuildSubdirList(BaseDir,List) // build List of all subdirectories under BaseDir
  76.    {                             // return count of elements in list
  77.       sprintf(SearchSpec,"%s\\*",BaseDir)
  78.       List = Directory(SearchSpec,TRUE,
  79.                        FATTR_RDONLY|FATTR_HIDDEN|FATTR_SYSTEM|FATTR_SUBDIR|FATTR_ARCHIVE,
  80.                        FATTR_SUBDIR)
  81.       return( ( List == NULL ) ? 0 : 1+GetArraySpan(List) );
  82.    }
  83.  
  84.    ReverseSortDirnames(Dir1,Dir2) // repeatedly called by qsort to sort Dir1 and Dir2
  85.    {
  86.       return( stricmp(Dir2.name,Dir1.name) );
  87.    }
  88.  
  89.    QuoteIfNeeded( Name ) // quote any directory names with spaces
  90.    {
  91.       QName = Name;
  92.       while ( Space = strchr(QName,' ') ) {
  93.          // space found in this directory, so quote between backslashes
  94.          Space[0] = '\0';
  95.          if ( !(BackSlash = strrchr(QName,'\\')) )
  96.             BackSlash = QName - 1;
  97.          Space[0] = ' ';
  98.          strcpy(BackSlash+2,BackSlash+1);
  99.          BackSlash[1] = '\"';
  100.          if ( !(BackSlash = strchr(BackSlash+1,'\\')) ) {
  101.             strcat(QName,"\"");
  102.             break;
  103.          }
  104.          strcpy(QName = BackSlash+1,BackSlash);
  105.          BackSlash[0] = '\"';
  106.       }
  107.    }
  108.  
  109.    DeleteAllFilesInDir(DirName) // call system to delete all files in this dir
  110.    {
  111.       printf("DEL %s\n",DirName)
  112.       system("echo Y | del %s > NUL",DirName)
  113.    }
  114.  
  115.    RemoveDirectory(DirName) // call system RMDIR call
  116.    {
  117.       printf("RMDIR %s\n",DirName)
  118.       system("RMDIR %s > NUL",DirName)
  119.    }
  120.  
  121. :CENVI_EXIT
  122. REM **********************
  123. REM *** Restore DELDIR ***
  124. REM **********************
  125.    ENDLOCAL
  126.    GOTO FINI
  127.  
  128. :SHOW_HOW
  129.    ECHO .
  130.    ECHO DelTree.cmd - Delete a subdirectory and all files and directories under it
  131.    ECHO .
  132.    ECHO USAGE: DelTree DirSpec [NOASK]
  133.    ECHO WHERE: NOASK: (optional) to not prompt "are you sure?"
  134.    ECHO        DirSpec: Specification for directory to delete
  135.    ECHO .
  136.    GOTO FINI
  137.  
  138. :FINI
  139.    SET UKEY=
  140.