start batch script execution on event

February 5, 2010 at 04:52:51
Specs: Windows XP, Core2Duo/2GB
Hi everybody,

I am working on a task, and I've faced this problem:
I have this directory Dir1 in which files are going to be stored periodically.
Now, I have this .bat script that should start, as soon as a new file has been stored in Dir1.

Any idea how can I do this?

Thnx in advance,
Pupli


See More: start batch script execution on event

Report •

#1
February 5, 2010 at 08:29:27
Depending on your CPU usage you could setup a MS-Scheduler to run every 5 seconds which would check the contents of the directory to see if a file was added. This would take up some of your computer resources to constantly be running.

There are third party apps to do this for you like Document Librarian or Data Warehousing software but they cost money.

If you go with the first option I suggested then you could use the following code.

@echo off
dir dir1 /b > currentdir.txt
set AllFilesMatch=1
For /F "tokens=* delims= " %%A in (currentdir.txt) do (
  set CurrentFileMatch=0
  For /F "tokens=* delims= " %%B in (olddir.txt) do (
    If %%A == %%B set CurrentFileMatch=1)
  If  %CurrentFileMatch%==0 set AllFilesMatch=0
)
copy currentdir.txt olddir.txt
If %AllFilesMatch%==0 echo Insert your code here.

******This is untested. *******

I will have to play with it later but it should give you some food for thought.


Report •

#2
February 5, 2010 at 10:34:49
Pupli, AFAIK, you can't do that, since you would have to trigger code that initiates on each file created in that directory ... and that should be a change to Windows itself.

Alternatively, you can do an asynchronous action, like running a script on regular basis, using a scheduler, or a service.


Report •

#3
February 5, 2010 at 12:50:23
might be able to shorten this:
For /F "tokens=* delims= " %%A in (currentdir.txt) do (
set CurrentFileMatch=0
For /F "tokens=* delims= " %%B in (olddir.txt) do (
If %%A == %%B set CurrentFileMatch=1)
If %CurrentFileMatch%==0 set AllFilesMatch=0
)
to:
fc currentdir.txt olddir.txt
if %errorlevel%==1 (
:: do operations here
move /y currendir.txt olddir.txt
)
or possibly just count the directory if files not being taken out:
dir /b dir1 | find /v /c "" > filct1
then compare the count of current vs previous.

Report •

Related Solutions

#4
February 8, 2010 at 07:20:53
VBScript can use WMI to monitor for the creation of files. I could try to find an example, but WMI's a pain, so I'm not going to bother if there's no interest.

Report •

#5
February 9, 2010 at 01:53:10
Thnx to all of you guys,

It would have been great to have the .bat script executed every time a file was stored in the folder, but I see what I'm looking is a little bit harder to find.

Rather, I'm trying in another way:

Firstly, I have scheduled the .bat file to be executed in a specific time.
Moreover, I will need to build a FOR loop, which is going to CALL the .bat file, as many time, as the number of files stored in the folder.
Does anyone have an idea, how can I achieve this?


Report •

#6
February 9, 2010 at 06:32:53
Try

for /f "tokens=*" %%a in ('dir /b') do (
  echo Execute on %%a)

Replace the Echo with your command.


Report •

Ask Question