home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / DELOLD.CMD < prev    next >
OS/2 REXX Batch file  |  1994-10-03  |  4KB  |  123 lines

  1. EXTPROC CEnvi2
  2. //*******************************************************
  3. //*** DelOld.cmd - Act like DEL, but delete file that ***
  4. //*** ver.1        are more than a certain number of  ***
  5. //***              days old.                          ***
  6. //*******************************************************
  7.  
  8. #include <OptParms.lib>
  9.  
  10. main(argc,argv)
  11. {
  12.    // get the command-line options
  13.    Prompt = OptionalParameter(argc,argv,"P");
  14.    Recurse = OptionalParameter(argc,argv,"R");
  15.    System = OptionalParameter(argc,argv,"S");
  16.    Hidden = OptionalParameter(argc,argv,"H");
  17.  
  18.    // get file name and age
  19.    FileSpec = argv[1];
  20.    Age = atoi(argv[2]);
  21.  
  22.    // if parameters weren't valid then print instructions and leave
  23.    if ( argc != 3  ||  Age < 1 ) {
  24.       Instructions();
  25.       return EXIT_FAILURE;
  26.    }
  27.  
  28.    // Whether or not recursion is selected, perform in the current
  29.    // directory
  30.    DelOld(FileSpec,Age,Prompt,System,Hidden);
  31.  
  32.    if ( Recurse ) {
  33.       // There can be A LOT of files if recursion is selected, and so
  34.       // do one directory at a time
  35.       FileParts = SplitFileName(FileSpec);
  36.       DirPart = FileParts.dir;
  37.       strcat(DirPart,"*");
  38.       DirList = Directory(DirPart,True,
  39.                           FATTR_RDONLY|FATTR_HIDDEN|FATTR_SYSTEM|FATTR_SUBDIR|FATTR_ARCHIVE,
  40.                           FATTR_SUBDIR);
  41.       if ( DirList ) {
  42.          // run DelOld in each directory
  43.          DirCount = 1 + GetArraySpan(DirList);
  44.          for ( i = 0; i < DirCount; i++ ) {
  45.             sprintf(Spec,"%s\\%s%s",DirList[i].name,FileParts.name,FileParts.ext);
  46.             DelOld(Spec,Age,Prompt,System,Hidden);
  47.          }
  48.       }
  49.    }
  50. }
  51.  
  52. DelOld(pFileSpec,pAge,pPrompt,pSystem,pHidden)
  53.    // delete each file matching FileSpec that is at least pAge old
  54. {
  55.    // Build list of matching files
  56.    Attrib = FATTR_RDONLY | FATTR_ARCHIVE;
  57.    if ( pSystem ) Attrib |= FATTR_SYSTEM;
  58.    if ( pHidden ) Attrib |= FATTR_HIDDEN;
  59.    FileList = Directory(pFileSpec,False,Attrib);
  60.  
  61.    if ( !FileList )
  62.       // no matching files were found, so return
  63.       return;
  64.  
  65.    // Get the current time, and translate old time to seconds because
  66.    // that is the simplest way to compare
  67.    AgeInSeconds = pAge * 24 * 60 * 60;
  68.  
  69.    FileCount = 1 + GetArraySpan(FileList);
  70.    for ( i = 0; i < FileCount; i++ ) {
  71.       if ( AgeInSeconds <= difftime(time(),FileList[i].Write) ) {
  72.          DeleteFile(FileList[i],pPrompt);
  73.       }
  74.    }
  75. }
  76.  
  77. DeleteFile(pFileData,pPrompt)
  78. {
  79.    strftime(TimeBuf,"%m-%d-%y",localtime(pFileData.Write));
  80.    // print name of file, date, and age in days
  81.    printf("%-58s %s Age: %.1f\n",pFileData.name,TimeBuf,
  82.           float(difftime(time(),pFileData.Write)) / (24 * 60 * 60) ); 
  83.  
  84.    // if rd_only then explain that cannot delete
  85.    if ( FATTR_RDONLY & pFileData.attrib ) {
  86.       printf("\n\a\tCANNOT DELETE READ-ONLY FILE!!!\n");
  87.    } else {
  88.       if ( pPrompt ) {
  89.          printf("Delete? (Y/N) ");
  90.          Answer = toupper(getch());
  91.          while ( !strchr("YN",Answer) ) {
  92.             printf("\a");
  93.             Answer = toupper(getch());
  94.          }
  95.          printf("%c\n",Answer);
  96.       }
  97.       if ( !pPrompt  ||  Answer == 'Y' )
  98.          remove(pFileData.name);
  99.    }
  100. }
  101.  
  102.  
  103.  
  104. Instructions()
  105. {
  106.    puts("\a")
  107.    puts(`DelOld - Delete old file; files more than X days old`)
  108.    puts(``)
  109.    puts(`USAGE: DelOld <FileSpec> <Age> [/P] [/R] [/S] [/H]`)
  110.    puts(``)
  111.    puts(`WHERE: FileSpec - Any file specification, including wildcards`)
  112.    puts(`       Age - Age, in days, of files to delete`)
  113.    puts(`       /P - Prompt before deleting each file`)
  114.    puts(`       /R - Recursive; work in current and subirectories`)
  115.    puts(`       /S - Include files with the SYSTEM attribute`)
  116.    puts(`       /H - Include files with the HIDDEN attribute`)
  117.    puts(``)
  118.    puts(`EXAMPLE: This will delete all *.bak file on C: at least 10 days old`)
  119.    puts(`            DelOld C:\*.bak 10 /R /S /H`)
  120.    puts(``)
  121. }
  122.  
  123.