///////////////////////////////////////////////////////////////////// // // This Task searches for a text string within files in a directory tree. // // Note: Search for {MODIFY} comments indicating items which need // to be modified or renamed (as appropriate). // ///////////////////////////////////////////////////////////////////// // // Variables used: // // %0 : Directory to process // (NOTE: First command line parameter else DEFINE value used) // %1 : File search pattern // (NOTE: Second command line parameter else DEFINE value used) // %2 : The text string to search for in the files. // (NOTE: Third command line parameter else DEFINE value used) // %6 : Matching file being check (loops through %DIR_FILE_% list) // %7 : Number of files deleted // %8 : Number of files processed // %9 : Server Console (base) Current Working Directory (CWD) // ///////////////////////////////////////////////////////////////////// // Open a log file to write what action is taken (APEND mode, no TRUNCATE) // (Specify TRUNCATE after the OPEN_WRITE file name to clear previous entries) OPEN_WRITE SYS:SYSTEM\SCANFILE.DAT IF ERRORLEVEL ECHO Unable to OPEN/CREATE the log file! ABORT END // Write a null line to separate previous entries WRITE // Echo to the Server Console and Write a header message identifying this action ECHO_WRITE %TASK_NAME% started on %MONTH%/%DAY%/%YEAR% at %HOUR%:%MINUTE%%AM_PM% // Store the Current Working Directory (CWD) in variable %9 for later return DEFINE %9 %CWD% // Set the number of files processed counter DEFINE %8 0000 // Set the number of files deleted counter DEFINE %7 0000 // {MODIFY} Text string to search for in the files // Can be passed as the third command line parameter for tasks // which are CALLed by another task or RUN from the Server Console // However, since scheduled tasks do not support command line // parameters, logic is included to DEFINE the Last Accessed Date // if a parameter is not passed on the command line or // for scheduled task operations. // // NOTE: Command line parameters are automatically assigned to the // %0 through %9 variables based upon the order specified. // If a text string containing spaces is passed as a // command line parameter, the text string needs to be // enclosed within quotes (""). Otherwise, each word in // the text string will be treated as a separate parameter. // Check if the text string was passed on the command line as the third // parameter (%2). If not, set a default text string. IF "%2"=="" DEFINE %2 text string END // {MODIFY} File Search Pattern // Can be passed as the second command line parameter for tasks // which are CALLed by another task or RUN from the Server Console // However, since scheduled tasks do not support command line // parameters, logic is included to DEFINE the File Search Pattern // if a parameter is not passed on the command line or // for scheduled task operations. // // NOTE: Command line parameters are automatically assigned to the // %0 through %9 variables based upon the order specified. // Check if the File Search Pattern was passed on the command line as // the second parameter (%1). If not, set a default File Search Pattern. IF "%1"=="" DEFINE %1 *.* END // {MODIFY} Directory to Process // Can be passed as the first command line parameter for tasks // which are CALLed by another task or RUN from the Server Console // However, since scheduled tasks do not support command line // parameters, logic is included to DEFINE the Directory to Process // if a parameter is not passed on the command line or // for scheduled task operations. // // NOTE: Command line parameters are automatically assigned to the // %0 through %9 variables based upon the order specified. // Check if the Directory to Process was passed on the command line as // the first parameter (%0). If not, set a default Directory to Process. IF "%0"=="" DEFINE %0 VOL:\PATH END // Change the Current Working Directory (CWD) to the Directory to be processed CD %0 IF ERRORLEVEL ECHO_WRITE Unable to change to Directory: %0! ABORT END // Check if any matching files exist in the specified Directory IF EXIST %1 // Clear the matching file variable (%6) before the first %DIR_FILE_% pass DEFINE %6 // WHILE/LOOP provides continuous processing while %DIR_FILE_% // continues to return a valid directory path WHILE // Get the first/next file matching the search pattern // NOTE: %DIR_FILE_% substitutes the first (when a search pattern // is supplied) or next (subsequent operations without a // search pattern) matching file in the directory. // Since we cleared %6 before entering the WHILE/LOOP, the // IF test will succeed on the first pass so that the // %DIR_FILE_%1% option is used (supplying a search pattern). // Subsequent LOOP operations will fall through to the // %DIR_FILE_% option (no search pattern supplied). IF "%6"=="" DEFINE %6 %DIR_FILE_%1% ELSE DEFINE %6 %DIR_FILE_% ENDIF // Have all of the matching files been checked (NULL return)? // NOTE: When all matching files have been exhausted, %DIR_FILE_% // will return a null value. IF "%6"=="" THEN BREAK // Increment number of files processed counter DEFINE %8 %8+=1 // Does this file contain the specified text string? IF SCAN_FILE "%2" %6 // It contains the specified text string // First we'll FLAG it RW -DI -RI... FLAG %6 rw // Then we'll delete it! DEL %6 // Check for error and echo result, // incrementing counter (on success) IF NOT ERRORLEVEL ECHO_WRITE %6 - DELETED! DEFINE %7 %7+=1 ELSE ECHO_WRITE %6 - ERROR: UNABLE TO DELETE! ENDIF // End of SCAN_FILE check structure ENDIF // Continue the file processing LOOP // END of IF EXIST %1 structure ENDIF // Display a summary of the results ECHO_WRITE. ECHO_WRITE Text String: "%2" ECHO_WRITE Directory: %0 ECHO_WRITE File Pattern: %1 ECHO_WRITE Files Checked: %8 ECHO_WRITE Files Deleted: %7 ECHO_WRITE. ECHO. // Reset the original Server Console Current Working Directory (CWD) CD %9 // Close the OPEN_WRITE log file CLOSE