WatchDirectory Forum
http://www.watchdirectory.net/cgi-bin/yabb25/YaBB.pl
watchDirectory forums >> How to... >> HowTo... Do 2 things...
http://www.watchdirectory.net/cgi-bin/yabb25/YaBB.pl?num=1057229664

Message started by Gert on Jul 3rd, 2003 at 6:54pm

Title: HowTo... Do 2 things...
Post by Gert on Jul 3rd, 2003 at 6:54pm
If you want to do more than 1 'thing' when a change in your monitored directory is detected you have several choices:
  • start multiple watchDirectory processes that monitor the same directory, each starting another bat-file.
    This has a few drawbacks:
    • Memory overhead (every watchDirectory process has the complete directory in memory)
    • CPU usage overhead (both watchDirectory processes do essentially the same)
    • Unpredictable outcome (there are no guarantees which bat-file will be executed first)

  • Combine all your actions into 1 bat-file that is started by 1 running watchDirectory process.
    This solution doesn't have the drawbacks of the previous solution, however:
    • Bat-files are hard to read as it is, keeping them small will help you to understand it when you have to change them 2 years from now

  • Use bat-file chaining
    Remember that you saw this term here first  ;)
    Let watchDirectory start a simple wrapper bat-file that does nothing more then calling 2 other bat-files which contain your real actions.
    There are no drawbacks when you use this method (that I am aware of).


A simple example of bat-file chaining
If you now have a bat-file that does 2 unrelated things that require a massive amount of code (half of it related to sending e-mail, the other half performing a backup of the new file).
The contents of your wrapper bat-file could be as simple as:

Code (]
@echo off
rem first call EMAIL.BAT to send e-mail
Call C:\WDBAT\EMAIL.BAT "gertrijs@gdps.dk" "Message from me"
rem and now do the backup
Call C:\WDBAT\BACKUP.BAT "D:\Backups"
rem done!
[/code):



The implementation of email.bat and backup.bat can use the normal environment-variables available to all bat-files that are started by watchDirectory.
The fun thing about this approach is that you can easily reuse these small bat-files in other watchDirectory bat-files. The email.bat file called above could look like the one below (based on the HowTo... Email) and it allows you to specify the recipient and subject on the command-line.
[code]
@echo off
rem who will receive this email
IF %1EMPTY == EMPTY SET EMAILTO=email@domain.com
IF NOT %1EMPTY == EMPTY SET EMAILTO=%1
rem what will be the subject
IF %2EMPTY == EMPTY SET SUBJ=%WD_REASON% %WD_FILE%
IF NOT %2EMPTY == EMPTY SET SUBJ=%2

echo %WD_REASON%  > "C:\TEMP\%WD_FILE_N%.blat"
echo %WD_FILE%    >> "C:\TEMP\%WD_FILE_N%.blat"

blat "c:\temp\%WD_FILE_N%.blat" -to %EMAILTO% -subject "%SUBJ%"
del "C:\TEMP\%WD_FILE_N%.blat"

Title: Re: HowTo... Do 2 things...
Post by Gert on Jul 5th, 2003 at 12:14am
"Anonymous" mailed me another way to create cleaner bat-files with watchDirectory:

Quote:
You could split the bat-file on the name of the event.
The wrapper could be structured as:
@echo off
Call C:\WDBAT\<dirname-that-is-monitored>_%WD_EVENT%.bat

And create separate bat-files (assuming the monitored dir is TOWATCH):
TOWATCH_FILENEW.BAT
TOWATCH_FILEDEL.BAT
etc.


I guess that this also helps to keep your bat-files small and focussed.
You could even combine the 2 techniques.

Gert

Title: Re: HowTo... Do 2 things...
Post by Farpoint on Apr 10th, 2005 at 12:48pm
Does anyone know how to create a pause in a .bat file? I use a .bat file to do my things but it is needed to sent two emails with a 30 second delay between them.

I can find no reference anyware on how to create a time of pause or wait 30 seconds statement using standard .bat commands.

In my .bat file it schould be:

do part one
wait 30 seconds
do part two

Herman
p.s. the statement 'pause' is not what I am looking for :-)

Title: Re: HowTo... Do 2 things...
Post by Farpoint on Apr 10th, 2005 at 1:07pm
The simplest of thing stick right under your nose :-)
Under windows 2003 you can use 'sleep X' in your batch file, but I still don't know how to do it under windows 2000.

Since last month my system runs under 2003 .......

Title: Re: HowTo... Do 2 things...
Post by Gert on Apr 10th, 2005 at 1:30pm

wrote on Apr 10th, 2005 at 12:48pm:
Does anyone know how to create a pause in a .bat file? I use a .bat file to do my things but it is needed to sent two emails with a 30 second delay between them.


Hello Herman,

In the directory where you have installed watchDirectory, there is a program called GdPUtil (http://www.watchdirectory.net/watchDir/GdPUtil.html.
One of its functions is to pause your .bat file.

If your .bat file is called by watchDirectory, you can use it like this:
[code]"%WD_INSTDIR%\GdPUtil.exe" -sleep 30
[/code]
WD_INSTDIR will automatically expand to the directory where watchDirctory is installed.

Gert

WatchDirectory Forum » Powered by YaBB 2.5.2!
YaBB Forum Software © 2000-2017. All Rights Reserved.