// // CLEANUP.TSK // 6/23/99 M.P.AVERY // // based on code delivered with TaskMaster // // Cleanup scans all volumes, deleting all .tmp files // and .bak files that are more than one day old, and // then purging all deleted files that were deleted more // than one day ago. // // This task will ONLY process volumes with long names // active. This is because I don't want to process cd-roms, // and all my real volumes have long name spaces. If you use // CD-ROM's with long name spaces, or use regular volumes without // long name spaces, you may have to reconsider the volume test. // Enable LOG ON for debugging purposes since it will // show the lines processed and their structure. // (Remove the leading slashes to enable LOG ON) // LOG ON // Change to the System Console screen before processing. CHANGE_SCREEN "System Console" // Clear the System Console screen. CLS // The following displays the currently mounted volumes // and their space usage on the console screen. VOLINFO // Capture the current System Console screen image as // text (default mode). SAVE_SCREEN SYS:SYSTEM\CONSOLE.DAT // Open the System Console screen text image for input. OPEN_READ SYS:SYSTEM\CONSOLE.DAT // Open an log file to log what action is taken OPEN_WRITE SYS:ETC\CLEANUP.DAT // This WHILE/LOOP scans for the string "Mounted Volumes" // in each line of the captured System Console screen. // Mounted Volumes is displayed on the line preceding // the Volume names shown by the VOLUMES console command. // (Note: The first pass fails since %1 is not defined.) WHILE NOT SCAN_STRING "Volume Name" "%1" // The "Volume Name" string was not found so read the next // line from the input file (System Console screen text image). READ %1 // Abort the Task if a read error occurs. IF ERRORLEVEL ECHO Read error occurred! ABORT ENDIF // If the read returns a null string, exit since the // System Console screen should be padded with spaces. // (Note: No spaces are allowed between the strings and // the operators in comparisons.) IF "%1"=="" ECHO Unable to locate volume information! ABORT ENDIF // Repeat the process until "Volume Name" or a null string. LOOP // The following WHILE/LOOP tests if the read string is null // The first pass it will not be since it matched above. // (Note: No spaces are allowed between the strings and the // operators in comparisons.) WHILE "%1">"" // Read the next line from the captured System Console screen READ %1 // Abort the Task if a read error occurs. IF ERRORLEVEL ECHO Read error occurred! ABORT ENDIF // If the string "%" is not found on the line, must be // out of volumes since ##% would appear as Used/Free. IF NOT SCAN_STRING "%" "%1" THEN BREAK // We only want to process real disks, not cd's, and since all our // real volumes have long name space on them, we'll test for the // long name space. This test won't work if you have volumes without // long name spaces, or cd-roms with long name spaces enabled. IF NOT SCAN_STRING "LONG" "%1" THEN CONTINUE // The following uses a quirk in the CD/CHDIR command: // Since a space is not a valid volume/path character, // the parser will ignore any trailing characters after // the specified volume/path. CD %1 // At this point, the .TSK has changed the Current Working // Directory (CWD) to the root of the (first/next) volume. // Another .TSK could be launched to walk the directory // tree and delete old/backup files, etc. or the logic // could be incorporated herein. // start the routine to clean up the current directory ///////////////////////////////////////////////////////////////////// // // This Task searches for .BAK & .TMP files which have not been // accessed on today's date and deletes them to make more Volume // space available. It could be spawned by any number of Server // monitoring packages in response to a Volume/Disk Space // threshold alert. // // Modified 6/22/99 by MP Avery so it won't use the same variables // as other routine to make it callable. Set it to process whatever // the current directory is, and to restore the starting current // directory. // ///////////////////////////////////////////////////////////////////// // // Variables used: // // %2 : Server Console (base) Current Working Directory (CWD) // %3 : CWD being processed (traverses %DIR_TREE_% tree) // %4 : File in CWD to check (loops through %DIR_FILE_% list) // %5 : Number of directories processed // %6 : Number of directories skipped // %7 : Number of files processed // %8 : Number of files deleted // %9 : Directory name to skip/bypass (optional) // Define this variable as the directory name to skip // use leading and trailing backslashes to insure that // the subdirectory (and subdirectories under it) are // skipped but not directories whose name happens to // start with this variable name (refer to examples) // (DOS format, case sensitive) // // Example 1: // DEFINE %9 // Note: This line can also be deleted since // the undefined default is a null value. // // results: // \PUBLIC\NLS (processed) // \PUBLIC\NLS\ENGLISH (processed) // \PUBLIC\NLS.OLD (processed) // \PUBLIC\NLSNAMES (processed) // // Example 2: // DEFINE %9 \NLS\ // // results: // \PUBLIC\NLS (skipped) // \PUBLIC\NLS\ENGLISH (skipped) // \PUBLIC\NLS.OLD (processed) // \PUBLIC\NLSNAMES (processed) // // Example 3: // DEFINE %9 \NLS // // results: // \PUBLIC\NLS (skipped) // \PUBLIC\NLS\ENGLISH (skipped) // \PUBLIC\NLS.OLD (skipped) // \PUBLIC\NLSNAMES (skipped) // ///////////////////////////////////////////////////////////////////// // 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% // save the current working directory DEFINE %2 %CWD% // change to the starting point (first directory to process) // better yet, don't // CD SYS:\ // number of directories processed DEFINE %5 0000 // number of directories skipped DEFINE %6 0000 // number of files processed DEFINE %7 0000 // number of files deleted DEFINE %8 0000 // if defined (not commented out), defines directory name to skip // DEFINE %9 // WHILE/LOOP provides continuous processing while %DIR_TREE_% // continues to return a valid directory path WHILE // Define the next matching entry in the directory tree // If this is the first directory checked, we must provide // a starting point to work from (. specifies the CWD) IF "%3"=="" DEFINE %3 %DIR_TREE_.% ELSE DEFINE %3 %DIR_TREE_% ENDIF // Was an excluded directory name defined? IF NOT "%9"=="" // Check for the excluded subdirectory specification WHILE SCAN_STRING "%9" "%CWD%\" ECHO_WRITE %CWD% (skipped) DEFINE %3 %DIR_TREE_% DEFINE %6 %6+=1 LOOP ENDIF // Has the entire tree been traversed (NULL return)? IF "%3"=="" THEN BREAK // Process this directory ECHO_WRITE %CWD% (processed) // Increment the Directories Processed counter DEFINE %5 %5+=1 // Do any .BAK files exist in this directory? IF EXIST *.BAK // Clear the variable for the first pass DEFINE %4 // WHILE/LOOP provides continuous processing while %DIR_FILE_% // continues to return a valid directory path WHILE // Get the first/next matching file in the search IF "%4"=="" DEFINE %4 %DIR_FILE_*.BAK% ELSE DEFINE %4 %DIR_FILE_% ENDIF // Have all the files been checked (NULL return)? IF "%4"=="" THEN BREAK // Increment number of files processed DEFINE %7 %7+=1 // Has this file has been accessed today? // %FILE_ACCESS_% format = (yyyy/mm/dd) IF "%FILE_ACCESS_%4%"<"%YEAR%/%MONTH%/%DAY%" // It has not been accessed today so // First we'll FLAG it RW -DI -RI... FLAG %4 rw // Then we'll delete it! DEL %4 // Check for error and echo result, // incrementing counter (on success) IF NOT ERRORLEVEL ECHO_WRITE %CWD%\%4 - DELETED! DEFINE %8 %8+=1 ELSE ECHO_WRITE %CWD%\%4 - ERROR: UNABLE TO DELETE! ENDIF // End of %FILE_ACCESS_% check structure ENDIF // Continue the file processing LOOP // End of IF EXIST *.BAK structure ENDIF // Do any .TMP files exist in this directory? IF EXIST *.TMP // Clear the variable for the first pass DEFINE %4 // WHILE/LOOP provides continuous processing while %DIR_FILE_% // continues to return a valid directory path WHILE // Get the first/next matching file in the search IF "%4"=="" DEFINE %4 %DIR_FILE_*.TMP% ELSE DEFINE %4 %DIR_FILE_% ENDIF // Have all the files been checked (NULL return)? IF "%4"=="" THEN BREAK // Increment number of files processed DEFINE %7 %7+=1 // Has this file has been accessed today? // %FILE_ACCESS_% format = (yyyy/mm/dd) IF "%FILE_ACCESS_%4%"<"%YEAR%/%MONTH%/%DAY%" // It has not been accessed today so // First we'll FLAG it RW -DI -RI... FLAG %4 rw // Then we'll delete it! DEL %4 // Check for error and echo result, // incrementing counter (on success) IF NOT ERRORLEVEL ECHO_WRITE %CWD%\%4 - DELETED! DEFINE %8 %8+=1 ELSE ECHO_WRITE %CWD%\%4 - ERROR: UNABLE TO DELETE! ENDIF // End of %FILE_ACCESS_% check structure ENDIF // Continue the file processing LOOP // End of IF EXIST *.TMP structure ENDIF // Continue the directory tree processing LOOP // Display a summary of the results ECHO_WRITE. ECHO_WRITE Directories: %5 processed / %6 skipped ECHO_WRITE Files: %7 processed / %8 deleted // Reset the original Server Console Current Working Directory (CWD) CD %2 // AND THEN PURGE THE VOLUME OF ALL FILES DELETED MORE THAN A DAY AGO PURGE /1 /A // Repeat the process until "%" is not found or a null string // is encountered (indicating an End Of File). LOOP // Clean up our work file. DEL SYS:SYSTEM\CONSOLE.DAT // CLOSE OPEN FILES CLOSE // Disable LOG ON (if previously enabled). // (Note: This is not required since TaskMaster will // clean up after the Task but it is a sign of // proper design and follow through in the logic.) // LOG OFF