WatchDirectory home page
WatchDirectory Startseite (Deutsche Version)
Site WatchDirectory (Français)
  Welcome, Guest. Please Login or Register
YaBB - Yet another Bulletin Board
   
  HomeHelpSearchLoginRegister  
 
Page Index Toggle Pages: 1
HowTo... Do 2 things... (Read 18677 times)
Gert
YaBB Administrator
*****
Offline



Posts: 2225
The Netherlands
HowTo... Do 2 things...
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  Wink
    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!
 



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"
 

Back to top
« Last Edit: Aug 19th, 2003 at 11:17pm by Forum Admin »  

Gert Rijs - gert (at) gdpsoftware (dot) com
Blog: http://blog-en.gdpsoftware.com/
End Alzheimer's: http://www.alz.org&&...
WWW WWW GdPSoftware  
IP Logged
 
Gert
YaBB Administrator
*****
Offline



Posts: 2225
The Netherlands
Re: HowTo... Do 2 things...
Reply #1 - 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
Back to top
 

Gert Rijs - gert (at) gdpsoftware (dot) com
Blog: http://blog-en.gdpsoftware.com/
End Alzheimer's: http://www.alz.org&&...
WWW WWW GdPSoftware  
IP Logged
 
Farpoint
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 2
Re: HowTo... Do 2 things...
Reply #2 - 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 Smiley
Back to top
 
 
IP Logged
 
Farpoint
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 2
Re: HowTo... Do 2 things...
Reply #3 - Apr 10th, 2005 at 1:07pm
 
The simplest of thing stick right under your nose Smiley
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 .......
Back to top
 
 
IP Logged
 
Gert
YaBB Administrator
*****
Offline



Posts: 2225
The Netherlands
Re: HowTo... Do 2 things...
Reply #4 - Apr 10th, 2005 at 1:30pm
 
Quote:
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
 


WD_INSTDIR will automatically expand to the directory where watchDirctory is installed.

Gert
Back to top
 

Gert Rijs - gert (at) gdpsoftware (dot) com
Blog: http://blog-en.gdpsoftware.com/
End Alzheimer's: http://www.alz.org&&...
WWW WWW GdPSoftware  
IP Logged
 
Page Index Toggle Pages: 1