//============================================================================= // // Task: FILEPOLL.TSK // (Check for existence of new files and process same accordingly) // Author: Avanti Technology, Inc. // http://www.avanti-tech.com // Version: 1.0 - Release [05 Aug 2003] Initial version // 1.1 - Updated [28 Sep 2006] Include new features // // Description: // ============ // Check for the existence of any new file(s) in a specific directory then // process the newly arrived file(s) accordingly. // (Note: In this example, each file is moved to a remote Server location.) // // While this task could make use of wild cards to simplify the copy process, // it uses the slightly more complicated technique in order to provide // individual file processing and user notification with logging. // // // Objective: // ========== // Process files placed in a specific directory with logging and notification. // // // Usage: // ====== // Script can be manually executed using the TaskMaster TMRUN command // at the TMConsole (Shell) Screen: // // Example: TMRUN [vol:path\]FILEPOLL.TSK // // Note: [vol:path\] is not required if the task resides either // in SYS:SYSTEM or the TaskMaster NLM load directory. // // Script can be scheduled for automatic execution using Client interface or // using the TaskMaster TMSCHEDULE command at the TMConsole (Shell) Screen: // // Examples: TMSCHEDULE ADD FILEPOLL.TSK 01 02:00 // (Executes the 1st of each month at 2:00am) // // TMSCHEDULE ADD FILEPOLL.TSK YNNNNNN 02:00 // (Executes every Sunday [SMTWTFS] at 2:00am) // // Note: Scheduled tasks must reside either in SYS:SYSTEM or the // TaskMaster NLM load directory for security reasons. // // // Notes: // ====== // There are several lines which need to be modified for the specific // environment where used (i.e., the Source Volume:\Path, the Destination // Server/Volume:\Path, etc.). These lines are preceded by comment lines // which start with [MODIFY]. Search the script for all lines with // [MODIFY] and change the command line which follows appropriately. // // This script was designed to be scheduled to run once per day, start of // the business day, and automatically terminate at 18:00 (6:00p). // // To change this design, modify or remove the following line at the end: // // IF "%HOUR24%%MINUTE%"<"1800" THEN GOTO START // // // Compatibility: // ============== // This task has been tested on the following platforms without demonstrating // any compatibility issues or any other reported/confirmed conflicts: // TaskMaster v4.13 (or later), TaskMaster Lite v4.13 (or later) // NetWare v5.x / v6.x / OES (NetWare kernel) // // Warning: // ======== // AS WITH ANY NEW SOFTWARE PROGRAM, BATCH SCRIPT, OR AUTOMATED PROCESSING // PROCEDURE, CAUTION SHOULD BE EXERCISED AND DUE DILIGENCE OBSERVED DURING // INITIAL IMPLEMENTATION. WHERE POSSIBLE, TESTING SHOULD BE PERFORMED ON // NON-PRODUCTION SYSTEMS PRIOR TO FULL IMPLEMENTATION. // // Comments: // ========= // This script is provided free of charge and without any warranty or // guarantee of fitness of purpose or performance. // // For additional TaskMaster script examples, visit the Sample Tasks page // on the Avanti Technology, Inc. WEB Site: http://www.avanti-tech.com // //============================================================================= // Check that the version of TaskMaster loaded is compatible (v4.13 or later) IF "%TM_VERSION%.%TM_SUBVERSION%%TM_REVISION%"<"4.13" ECHO. ECHO Error: Incompatible TaskMaster release (requires v4.13 or later)! ECHO. ABORT ENDIF // Check is this task is already running (only need one active copy to work) IF ACTIVE_TASK %TASK% THEN EXIT // All processing is logged in a file of the same name as the currently // running task file but with an extension of .LOG which is located in // the same VOL:PATH as the currently running task file. // (i.e., %TASK_FILE%.TSK / %TASK_FILE%.LOG both located in %TASK_PATH%) OPEN WRITE #0 %TASK_PATH%\%TASK_FILE%.LOG // Check for success in creating/opening the log file. IF ERRORLEVEL // Define the error message to be sent (NetWare SEND / SMTP E-Mail) DEFINE %ERR_MSG% "Cannot Create/Open Log - Processing Terminated!" // Define label (script section) where processing should continue // after the error notification (in this case, EOJ or exit/terminate) DEFINE %NEXTSTEP% EOJ // Jump to the script section which provides error alerts // Note: Cannot log since the log file could not be accessed. GOTO SEND_ALERT ENDIF // Assign alias names to default internal dynamic variables for simplicity VARALIAS %VAR10% %SRC_PATH% VARALIAS %VAR11% %DST_PATH% VARALIAS %VAR13% %ERR_CNT% VARALIAS %VAR14% %ERR_MSG% VARALIAS %VAR15% %FILENAME% VARALIAS %VAR16% %NEXTSTEP% VARALIAS %VAR17% %XFER_CNT% // [MODIFY] Define the Volume:\Path to poll for the arrival of new file(s) // Note: Exclude trailing backslash in path (i.e., SYS: or SYS:\SYSTEM) DEFINE %SRC_PATH% VOL:\PATH // Verify the existence of the %SRC_PATH% IF NOT EXIST "%SRC_PATH%" // Define the error message to be logged/sent (NetWare SEND / SMTP E-Mail) DEFINE %ERR_MSG% "Invalid Source Path - Processing Terminated!" // Define label (script section) where processing should continue // after the error notification (in this case, EOJ or exit/terminate) DEFINE %NEXTSTEP% EOJ // Jump to the script section which provides error logging/alerting GOTO LOG_ALERT ENDIF // [MODIFY] Define the Server/Volume:\Path where the file(s) should be moved // Note: Exclude trailing backslash in path (i.e., FS1/SYS: or FS1/SYS:\SYSTEM) DEFINE %DST_PATH% SERVER/VOL:\PATH // Verify the existence of the %DST_PATH% IF NOT EXIST "%DST_PATH%" // Define the error message to be logged/sent (NetWare SEND / SMTP E-Mail) DEFINE %ERR_MSG% "Invalid Destination Path - Processing Terminated!" // Define label (script section) where processing should continue // after the error notification (in this case, EOJ or exit/terminate) DEFINE %NEXTSTEP% EOJ // Jump to the script section which provides error logging/alerting GOTO LOG_ALERT ENDIF // Define the transferred file count DEFINE %XFER_CNT% 0 // Define the count of errors encountered DEFINE %ERR_CNT% 0 // Log the start of processing WRITE #0 %MONTH%/%DAY%/%YEAR% @ %HOUR%:%MINUTE%%AM_PM% - Processing started... WRITE #0 // Here is the start of the script section which actually polls for and // processes any new file(s) :POLL // Clear %FILENAME% for use in new file processing DEFINE %FILENAME% // Check for new file(s) WHILE EXIST "%SRC_PATH%\*.*" // File(s) added to Source directory so process accordingly // In this example, the file(s) are moved (copied/deleted) to a // Remote Server directory. This could be done in one quick // sweep using SXCOPY and wild cards. However, we will collect // and process the files individually to better provide for // logging and error recovery. // Check if %FILENAME% is NULL which indicates this is the first loop // in which case we must specify a search specification for %DIR_FILE_% IF "%FILENAME%"=="" // Use %FILENAME% to build the search specification for %DIR_FILE_% DEFINE %FILENAME% %SRC_PATH%\*.* // First / initial loop using %DIR_FILE_% requires search specification DEFINE %FILENAME% %DIR_FILE_%FILENAME%% ELSE // Subsequent / next loops using %DIR_FILE_% use previous specification DEFINE %FILENAME% %DIR_FILE_% ENDIF // Check if %FILENAME% is NULL (i.e., indicates no more matching files) // and terminate (break) this processing loop if finished IF "%FILENAME%"=="" THEN BREAK // Build full path specification from %SRC_PATH% and retrieved %FILENAME% DEFINE %FILENAME% %SRC_PATH%\%FILENAME% // Convert the %SRC_PATH% and retrieved %FILENAME% (DOS) to LONG Name Space DEFINE %FILENAME% %FILE_NAME_LONG_%FILENAME%% // Copy the new file to the desired destination // (Server-to-Server copy requires SCOPY / SXCOPY) SXCOPY "%FILENAME%" "%DST_PATH%" IF NOT ERRORLEVEL // SUCCESS: Log activity, remove file and increment file counter WRITE #0 %MONTH%/%DAY%/%YEAR% @ %HOUR%:%MINUTE%%AM_PM% WRITE #0 %FILENAME% WRITE #0 -> %DST_PATH% WRITE #0 DELETE "%FILENAME%" DEFINE %XFER_CNT% %XFER_CNT%+=1 ELSE // FAILURE: Define error message, log it and send an alert then // return, incrementing error count, before checking for more files DEFINE %ERR_MSG% %FILENAME% - SXCOPY Failed (Will retry next cycle)... DEFINE %NEXTSTEP% POLL_ERROR_RETURN GOTO LOG_ALERT :POLL_ERROR_RETURN DEFINE %XFER_CNT% %XFER_CNT%+=1 ENDIF LOOP // Wait for the next interval to resume processing // [MODIFY[ Adjust the delay between polling checks accordingly (in seconds) SLEEP 60 // Check the time and continue processing if not too late (terminate at 6:00pm) // [MODIFY] Adjust the termination time accordingly IF "%HOUR24%%MINUTE%"<"1800" THEN GOTO POLL // Summarize the processing activity WRITE #0 WRITE #0 %MONTH%/%DAY%/%YEAR% @ %HOUR%:%MINUTE%%AM_PM% - Processing complete... WRITE #0 WRITE #0 Copied: %XFER_CNT% WRITE #0 Errors: %ERR_CNT% WRITE #0 WRITE #0 // Close the log file CLOSE WRITE #0 // Save log file (rename from .LOG to .### - Julian date extension) RENAME %TASK_PATH%\%TASK_FILE%.LOG *.%NDAY_OF_YEAR% // (End Of Job) :EOJ EXIT // Script section to log any errors :LOG_ALERT // Record the error in the log file WRITE #0 WRITE #0 %MONTH%/%DAY%/%YEAR% @ %HOUR%:%MINUTE%%AM_PM% WRITE #0 %ERR_MSG% WRITE #0 // Script section to send alerts regarding any errors // Note: If alerting is not required, comment/delete the next script section // up to but not including the GOTO %NEXTSTEP% commend line. :SEND_ALERT // Create/open (truncate) temporary file to write E-Mail message OPEN WRITE #1 %TASK_PATH%\%TASK_FILE%.EML TRUNCATE // Check status for creating/opening file and revert to NetWare SEND on failure IF NOT ERRORLEVEL // Create SMTP message header // [MODIFY] To/From and any other fields desired accordingly WRITE #1 To: user@domain.com WRITE #1 From: user@domain.com WRITE #1 Subject: %TASK% Error WRITE #1 WRITE #1 %MONTH%/%DAY%/%YEAR% @ %HOUR%:%MINUTE%%AM_PM% WRITE #1 %TASK%: %ERR_MSG% WRITE #1 // Close the temporary E-Mail file CLOSE WRITE #1 // Send the E-Mail message via SMTP // [MODIFY] Replace brackets & contents with SMTP Server URL or IP address TMSMTP [smtp.domain.com | 127.0.0.1] %TASK_PATH%\%TASK_FILE%.EML // Check status and clear %ERR_MSG% if successful IF NOT ERRORLEVEL THEN DEFINE %ERR_MSG% // Cleanup (delete temporary SMTP E-Mail message file) DELETE %TASK_PATH%\%TASK_FILE%.EML ENDIF // Check if %ERR_MSG% is null ("") which indicates successful SMTP send else // revert to NetWare SEND for alerting // Note: If SMTP Server access is not available and/or NetWare SEND alerts // are preferred, comment out or remove the command lines in this script // section starting at OPEN WRITE #1 up to this section of comments. // The IF NOT "%ERR_MSG% line and the accompanying ENDIF can also be // removed (since without the other section %ERR_MSG% will always be // non-NULL - i.e., have an erro message) and the commands nested in // the section between the removed IF / ENDIF lines left justified. IF NOT "%ERR_MSG%"=="" // Fall back alerting method if SMTP fails or is unavailable // [MODIFY] Specify User(s) to be alerted via NetWare SEND SEND "%TASK%: %ERR_MSG%" TO ADMIN // Clear %ERR_MSG% to indicate alerting complete DEFINE %ERR_MSG% ENDIF // Jump/return to specified script section GOTO %NEXTSTEP%