WatchDirectory Newsletter - Detecting an outbreak of new files

thumbnail - monitor folders

At most once a month we send a newsletter to keep you informed about watchDirectory.
You can unsubscribe anytime you want. We care about your privacy and will not make your email available to other parties.

Subscribe to the English language newsletter
Subscribe Unsubscribe
Den Deutschen Newsletter abonnieren
Abonnieren Kündigen

November 2005 Detecting an outbreak of new files

Recently a customer asked whether watchDirectory can take action when more than 30 files were created during the last 10 minutes. When less files were created, nothing should happen. When more files were created, watchDirectory should send an e-mail alert.

For this example, you should configure a new task that runs a batch file for all new files detected in the watched directory. The batch file will read the history database (the same as used by the Task History Viewer) to check how many files were created the last 10 minutes. When more than 30 files were created, the batch file will send an e-mail.

The outbreak.bat script

The example script uses a program called sqlite3.exe, which is part of the excellent SQLite database engine used by watchDirectory. The sqlite3 program is included in the download at the bottom of this page.
The script sends e-mail using the technique described here.

rem path to sqlite3.exe (no "quotes")
SET SQL=C:\outbreak\sqlite3.exe

rem time interval to use in minutes
SET MINUTES=10
rem the number of files allowed before triggering an email
SET MAXFILE=30
rem who should receive the email alert?
SET EMAIL=johnny@somewhere.net

rem ===============================================
rem ==== no need to change anything below here ====
rem ===============================================

rem create temporary files inside C:\Documents and Settings\All Users\Application Data\watchDirectory\taskname\...
SET TFBASE=%WD_TASKDIR%\F1%WD_CUREVT%

rem compute the date/time of MINUTES minutes ago
"%WD_INSTDIR%\GdPUtil.exe" -datetime -%MINUTES%m "%TFBASE%.bat"
call "%TFBASE%.bat"
del "%TFBASE%.bat"

rem create the query
SET TMST='%DT_YEAR%-%DT_MONTH%-%DT_DAY% %DT_HOUR%:%DT_MIN%:%DT_SEC%'
rem always take care with pipe and "greater than" characters (escape them)
ECHO Select 'set numfiles=' ^|^| count(*) from WDEVENTS where DTEVENT ^> %TMST%; > "%TFBASE%.SQL"

rem and start the query
type "%TFBASE%.SQL" | "%SQL%" "%WD_TASKDIR%\history.db" > "%TFBASE%NF.BAT"
del "%TFBASE%.SQL"
call "%TFBASE%NF.BAT"
del "%TFBASE%NF.BAT"
if %numfiles% LSS %MAXFILE% (
   rem less than MAXFILE files were created the last MINUTES minutes
   goto :EOF
)
rem more than MAXFILE files! send an email alert
set EMAILFILE=%WD_TASKDIR%\..\System.Mail\Out\%WD_CONFIG%_%WD_CUREVT%.TMP
echo ALERT: %numfiles% files created in directory %WD_DIR% > "%EMAILFILE%"
echo %EMAIL% >> "%EMAILFILE%"
echo. >> "%EMAILFILE%"
echo. >> "%EMAILFILE%"
echo. >> "%EMAILFILE%"
echo. >> "%EMAILFILE%"
echo Hi, >> "%EMAILFILE%"
echo During the last %MINUTES% minutes there were %numfiles% files created inside directory %WD_DIR% >> "%EMAILFILE%"
rem okay, the script is created, now rename it so the wdPostMan program processes it.
ren "%EMAILFILE%" %WD_CONFIG%_%WD_CUREVT%.wdmail

Download the outbreak example. Unzip all files into a new directory. C:\outbreak would be easiest as the example script is configured to use that directory.


German Website deutschsprachige Webseite      French Website French Language Website      Spanish Website Spanish Language Website