/////////////////////////////////////////////////////////////////////
// This Task file searches for .BAK & .TMP files which can be deleted
// so as to make more Volume space available.  It could be spawned by
// any number of network monitoring packages in response to a Volume/
// Disk space at threshold alert.
/////////////////////////////////////////////////////////////////////
 
 
// Open an output file to log what action is taken
 
OPEN_WRITE SYS:SYSTEM\PURGE.DAT
 
 
// Write a null line to separate previous entries
 
WRITE
 
 
// Echo and Write a header identifying this action
 
ECHO_WRITE %TASK_NAME% started on %MONTH%/%DAY%/%YEAR% at %HOUR%:%MINUTE%%AM_PM%
 
 
// Variables used:
//
//   %0 : Directory search starting point
//   %1 : Next matching directory in search
//   %2 : Next matching file in search
//   %9 : Original Working Directory
 
 
// Save the Current Working Directory (CWD)
 
DEFINE %9 %CWD%
 
 
// Set %1 to 'nul' (an invalid return from %DIR_TREE_%) 
// to isolate the first pass of the %DIR_TREE_% request.
 
DEFINE %1 nul
 
 
// Set %0 to the starting point to begin traversing
 
DEFINE %0 SYS:\
 
 
// Set the Current Working Directory (CWD) to the
// starting point for the directory tree traversing
 
CD %0
 
 
// Directory traversing uses this WHILE/LOOP
 
WHILE "%1">""
 
 
  // Check if any .BAK files exist in this directory
 
  IF EXIST *.BAK
 
 
    // Define variable %2 as the first matching
    // file in the search
 
    DEFINE %2 %DIR_FILE_*.BAK%
 
 
    // For file checking, uses a WHILE/LOOP which
    // loops while %2 is defined
 
    WHILE "%2">""
 
 
      // Check if this file has been accessed today
      // %FILE_ACCESS_% format = (yyyy/mm/dd)
 
      IF "%FILE_ACCESS_%2%"<"%YEAR%/%MONTH%/%DAY%"
 
 
        // Echo/Write a message on what we're about to do
 
        ECHO_WRITE %CWD%\%2 (Owner: %FILE_OWNER_%2%) deleted!
 
 
        // It has not been accessed today so
        // First we'll FLAG it RW -DI -RI...
 
        FLAG %2 rw
 
 
        // Then we'll delete it!
 
        DEL %2
 
 
      // End of %FILE_ACCESS_% check structure
 
      ENDIF
 
 
      // Get the next matching file
 
      DEFINE %2 %DIR_FILE_%
 
 
    // Repeat the process
 
    LOOP
 
 
  // End of IF EXIST *.BAK structure
 
  ENDIF
 
 
  // Check if any .TMP files exist in this directory
 
  IF EXIST *.TMP
 
 
    // Define variable %2 as the first matching
    // file in the search
 
    DEFINE %2 %DIR_FILE_*.TMP%
 
 
    // For file checking, uses a WHILE/LOOP which
    // loops while %2 is defined
 
    WHILE "%2">""
 
 
      // Check if this file has been accessed today
      // %FILE_ACCESS_% format = (yyyy/mm/dd)
 
      IF "%FILE_ACCESS_%2%"<"%YEAR%/%MONTH%/%DAY%"
 
 
        // Echo/Write a message on what we're about to do
 
        ECHO_WRITE %CWD%\%2 (Owner: %FILE_OWNER_%2%) deleted!
 
 
        // It has not been accessed today so
        // First we'll FLAG it RW -DI -RI...
 
        FLAG %2 rw
 
 
        // Then we'll delete it!
 
        DEL %2
 
 
      // End of %FILE_ACCESS_% check structure
 
      ENDIF
 
 
      // Get the next matching file
 
      DEFINE %2 %DIR_FILE_%
 
 
    // Repeat the process
 
    LOOP
 
 
  // End of IF EXIST *.TMP structure
 
  ENDIF
 
 
  // Define variable %1 as the next matching entry in the
  // directory tree as it is traversed by %DIR_TREE_%.  If
  // this is the first directory checked, %1 will be null.
  // Therefore, we must provide a starting point for the
  // %DIR_TREE_% System Environment variable to work from
  // (Note: %0 was previously defined as the search starting
  // point and is used so that it only need be defined once.)
 
  IF "%1"=="nul"
    DEFINE %1 %DIR_TREE_%0%
  ELSE
    DEFINE %1 %DIR_TREE_%
  ENDIF
 
 
// LOOP back up to the WHILE check for the next directory tree level
 
LOOP
 
 
// Return to the saved original directory
 
CD %9
 
 
// Close the output file (not required since TaskMaster will
// cleanup after a Task but shows good design and structure)
 
CLOSE
