home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / DELOLD.CMM < prev    next >
Text File  |  1996-01-24  |  4KB  |  125 lines

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