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
rem the number of files allowed before triggering an email
rem who should receive the email alert?

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\...

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
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"
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
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.

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