1.

Solve : No activity monitor in a folder?

Answer»

Hi Guys
I am bit new in Bat scripts . I need a script which will monitor a folder and if there is no activity in that folder ( creation of file , delete of file) for about 10 mins it will show a message in the screen ...

Is that possible ?

Many thanks in Advance ..
Ashraf try:

Code: [Select]:loop
set timee=%time:~0,-3"%
set /a timee=%timee%-10
dir /tc | find "%date%" "%timee%"
dir /ta | find "%date%" "%timee%"
dir /tw | find "%date%" "%timee%"
pause
choice /m "continue?"
if errorlevel1 goto loop
if errorlevel0 goto EOF

FBThanks For a quick reply ..awsome ..
will you please spend some of ur time for me to explain that .....

Many Thanks mate ..Looking forward to the explanation too I am not aware that batch code can monitor events. You can use VBScript instead:

Code: [Select]strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceOperationEvent WITHIN 3 WHERE " _
& "Targetinstance ISA 'CIM_Datafile' and " _
& "TargetInstance.Drive = 'c:' And "_
& "TargetInstance.Path= '\\scripts\\'")
Do
Set objLatestEvent = colMonitoredEvents.NextEvent()
'WScript.Echo objLatestEvent.Path_.Class & " " & objLatestEvent.TargetIn
stance.Name

Select Case objLatestEvent.Path_.Class
Case "__InstanceCreationEvent"
WScript.Echo "File Created: " & objLatestEvent.TargetInstance.Name
Case "__InstanceDeletionEvent"
WScript.Echo "File Deleted: " & objLatestEvent.TargetInstance.Name
Case "__InstanceModificationEvent"
WScript.Echo "File Modified: " & objLatestEvent.TargetInstance.Name
End Select
Loop

Save the script with a vbs extension. RUN from the command prompt as cscript scriptname.vbs The directory is set to c:\scripts. Change as necessary.

Good luck.

The choice utility did not ship with XP (the OP's OS) and early versions of choice do not have the /m switch.I was hoping for an explanation of how SET /A would deduct 10 (minutes) from %timee% which is in the format hh:mm:ss - I think all I got from that line of the bat script was "missing operand"

set /a r=%time:~3,1%-1yup sorry i was drunk when i wrote it. It is possible to do it...

Code: [Select]:loop
set timee=%time:~0,-6"%
for /f "tokens=1-2 delims=:" %%A in ("%timee%") do (
set /a B=%%B-10
set timee=%%A:%%B
)
dir /tc | find "%date%" "%timee%"
dir /ta | find "%date%" "%timee%"
dir /tw | find "%date%" "%timee%"
pause
choice /m "continue?"
if errorlevel1 goto loop
if errorlevel0 goto EOF
unfortunately this too has it's drawback, it won;t work at times under ten past the hour and even if you put in a clause for that you'd have to put in another for times under ten past midnight. it's just better to use VBA

sorry

FBOr in fact as diablo says:
Quote from: diablo416 on September 10, 2008, 04:28:39 PM

set /a r=%time:~3,1%-1

FBThanks for the all the reply .
I was trying to do the VB script as sidewinder says and got the folloing error :

C:\>cscript no.vbs
C:\no.vbs(12, 1) Microsoft VBScript runtime error: Object required: 'stance'

Is taht make any sence ?

Sorry about that. The line in question was a comment.

Code: [Select]strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceOperationEvent WITHIN 3 WHERE " _
& "Targetinstance ISA 'CIM_Datafile' and " _
& "TargetInstance.Drive = 'c:' And "_
& "TargetInstance.Path= '\\scripts\\'")
Do
Set objLatestEvent = colMonitoredEvents.NextEvent()
Select Case objLatestEvent.Path_.Class
Case "__InstanceCreationEvent"
WScript.Echo "File Created: " & objLatestEvent.TargetInstance.Name
Case "__InstanceDeletionEvent"
WScript.Echo "File Deleted: " & objLatestEvent.TargetInstance.Name
Case "__InstanceModificationEvent"
WScript.Echo "File Modified: " & objLatestEvent.TargetInstance.Name
End Select
Loop

This script was designed to issue a message whenever an event occurred in the c:\script directory.

You will need to modify the script to monitor no events happening. One way would be to note the times any events OCCUR. When the time difference between two events exceeds 10 minutes, you'll have your period of inactivity.

Mostly, monitor scripts (you can monitor most anything) are used to trigger another event or to create documentation of some activity. Hmmm, a new concept, a monitor script to monitor non-events.

Thanks Sidewinder

I am not that smart to do what you are saying as I am not from a computer background ...Is it possible to implement by you , what you are saying (One way would be to note the times any events occur. When the time difference between two events exceeds 10 minutes, you'll have your period of inactivity.) ..It might be very easy code for you and will take 5 mins but to me ..it seems very complex ..

Thanks for helping me so far Hi Sidewinder

Also with my other script I have ..I use bmail (which is command line mail ) to send me a email then exceed the thresold .I have used that in batch script before and working good .(Dont know how you can use that in VB Script)

Is it also possible to send email using bmail program when there is no activity in the folder for 10 mins ...?

This script will be very help full for me ...of what I am doing right the moment ...

Sorry to be a pain for asking you so many things .

Many Thanks in advance ...
A monitor script is designed to wait for an event to occur and then do some work. In no event occurs, no work is performed. You can see the dilemma of monitoring non-events.

I was able to get these two scripts to work in a controlled environment. The first script monitors and times stamps directory activity. The second script calculates the time difference between the last event and the CURRENT time. When the difference gets to ten, we have liftoff


Script 1

Code: [Select]Const ForWriting = 2

strComputer = "."

Set fso = CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceOperationEvent WITHIN 3 WHERE " _
& "Targetinstance ISA 'CIM_Datafile' and " _
& "TargetInstance.Drive = 'c:' And "_
& "TargetInstance.Path= '\\scripts\\'")
Do
Set objLatestEvent = colMonitoredEvents.NextEvent()
Set f = fso.OpenTextFile("c:\temp\Timer.txt", ForWriting, True)
Select Case objLatestEvent.Path_.Class
Case "__InstanceCreationEvent"
f.WriteLine Now
Case "__InstanceDeletionEvent"
f.WriteLine Now
Case "__InstanceModificationEvent"
f.WriteLine Now
End Select
f.Close
Loop

Script 2
Code: [Select]Const ForReading = 1
Set WshShell = CreateObject("Wscript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

Do
Set f = fso.OpenTextFile("c:\temp\Timer.txt", ForReading)
strTime = f.ReadLine
If DateDiff("n", CDate(strTime), Now) > 10 Then
WshShell.Run "bmail ........",,True
Exit Do
End If
f.Close
WScript.Sleep 30000
Loop

I'm not familiar with bmail so you need to fill in the dotted line.

Good luck.

Do not put the scripts in the directory being monitored.Thanks a Lot sidewinder ..
My problem has been FIXED ..

Thanks to all for putting suggestion and help me out ..
This was my first post on the forum and I have started like this forum already , because of u all helping ppl over there ..

Thanks again


Discussion

No Comment Found