# VB Script to monitor folder modified

Hi,
Currently I´m monitoring some critical transfer folders with the attached script, this is working fine, but there are some folders that only hosts files for a few seconds, so now I need that it alerts if the folder has been modified, because on the tests that I have made I can not detect the changes, only when the folder has been modified, so, I´d like to know if there´s a way to send an alarm do it, maybe an error code or an statistic.

' Set base folder to monitor
Const conBaseDir = "\\xxx.xxx.xxx.xxx\E\$\Folder"
Const conInterval = 5

' Create file system object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Access folder we are monitoring
Set objFolder = objFSO.GetFolder(conBaseDir)

' Initialize flags
intReturn = 0
intCount = 0

' See if there are any files in the folder currently
For Each objFile In objFolder.Files
' Check if older than 5 minutes, if so indicate "old" files
If DateDiff("n", objFile.DateCreated, Now) > conInterval Then
Wscript.Echo "Message: File older than 5 mins: [" & objFile.Name & "], Created at: [" & objFile.DateCreated & "]"
intReturn = 1
intCount = intCount + 1

Else
' Otherwise just indicate there are new files
Wscript.Echo "Message: File added to queue: [" & objFile.Name & "], Created at: [" & objFile.DateCreated & "]"
If intReturn = 0 Then intReturn = 2
intCount = intCount + 1
End If
Next

' If no files found in queue, see if we might have missed some since last check
If intReturn = 0 Then
If DateDiff("n", objFolder.DateLastModified, Now) < conInterval Then
WScript.Echo "Message: No new files found, but queue modified at: [" & objFolder.DateLastModified & "]"
intReturn = 0
intCount = intCount + 1
End If
End If

' Return appropriate return code to calling program
WScript.Echo vbCrLf & "Statistic:" & intCount
WScript.Quit(intReturn)

###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

IT / Software Engineering ConsultantCommented:
Did a test here, script works fine.  Below is the output from my test.  I created a folder named "x:\ee\EE26938811\dir", waited over 5 minutes, and ran the VBS.  It only displayed the Statistics:0 message.  Then I created a file in the folder, and deleted it, to force an update to the folder last changed date.  Rerunning the VBS it messaged that the folder had been changed, and returned the correct statistic.  What are you seeing different than this?

X:\ee\EE26938811>dir
Volume in drive X is Data
Volume Serial Number is F40B-7077

Directory of X:\ee\EE26938811

04/06/2011  09:27 PM    <DIR>          .
04/06/2011  09:27 PM    <DIR>          ..
04/06/2011  09:26 PM    <DIR>          dir
04/06/2011  09:27 PM             1,476 EE26938811.vbs
1 File(s)          1,476 bytes
3 Dir(s)  15,485,386,752 bytes free

X:\ee\EE26938811>cscript EE26938811.vbs

Statistic:0

X:\ee\EE26938811>echo.>dir\test.txt

X:\ee\EE26938811>del dir\test.txt

X:\ee\EE26938811>dir
Volume in drive X is Data
Volume Serial Number is F40B-7077

Directory of X:\ee\EE26938811

04/06/2011  09:27 PM    <DIR>          .
04/06/2011  09:27 PM    <DIR>          ..
04/06/2011  09:33 PM    <DIR>          dir
04/06/2011  09:27 PM             1,476 EE26938811.vbs
1 File(s)          1,476 bytes
3 Dir(s)  15,485,386,752 bytes free

X:\ee\EE26938811>cscript EE26938811.vbs
Message: No new files found, but queue modified at: [04/06/2011 9:33:58 PM]

Statistic:1

~bp

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Hi billprew,

Thanks as always for your help.

The output information is correct, in fact, the script is running pretty fine, the folder modified information shows ok and it has been a very huge help. The thing is that there are 4 folders that I need to monitor due their critical mission and our monitoring software runs the script every 60 seconds at minimum, so it shows me the modified date/time of the folder as a message and it is ok, but I can´t generate an alarm with the message for those special folders because I need to obtain an output of the date/time information as an exit code, for example, please see this script that checks a file when it has been modified. The idea is doing the same but with a folder and thanks again:
Usage: FILEPATH  Local or UNC Path of the target file ) STATEDIR ( A Directory where a state file used by the script each run may be stored)
Const  FAIL = 1, SUCCESS = 0
Dim lstArgs, STATE_PATH
Dim boolStateFound
Dim strFileName, strStateFile, strStateFound, strDateModified, strStateDate, strStateSize, strSize
Dim objFso, objFile, objStateFile, objStream

'get arguments
Set lstArgs = WScript.Arguments

If lstArgs.Count = 2 Then

strFileName  = Trim( lstArgs( 0 ))
STATE_PATH   = Trim( lstArgs( 1 ))

Else

WScript.Echo "Message: Usage: FILE_NAME STATEDIR" & vbCRLF & "FILE_NAME = Local or UNC Path of the target file"
WScript.Echo "Statistic: 0"
WScript.Quit( FAIL )

End If

Set objFso = Wscript.CreateObject( "Scripting.FileSystemObject" )

'check for the existence of the file, and exit if not found
If Not objFso.FileExists(strFileName) Then

WScript.Echo "Statistic: 0"
WScript.Quit( FAIL )

End If

'get the file's modified date
Set objFile = objFso.GetFile(strFileName)
strDateModified = CStr(objFile.DateLastModified)
strSize         = CStr(objFile.Size)

If Not objFso.FolderExists(STATE_PATH) Then

objFso.CreateFolder(STATE_PATH)

End If

'create the state file name
If InStr(strFileName, "\\") = 1 Then

strStateFile = STATE_PATH & Replace(strFileName, "\", ".", 3) & ".state"

Else

strStateFile = STATE_PATH & Replace(strFileName, "\", ".") & ".state"

End If

'now remove spaces and colons from the path (if any exist)

strStateFile = Replace(strStateFile, ":", "")

boolStateFound = true

'check for the existence of the corresponding state file and create if not found
If Not objFso.FileExists( strStateFile ) Then

objFso.CreateTextFile(strStateFile)
boolStateFound = false

End If

If boolStateFound Then
'retrieve the state info

Set objStateFile = objFso.OpenTextFile(strStateFile, 1, false)
objStateFile.close

'compare with the last known modification
If strStateDate <= strDateModified And strSize = strStateSize Then

WScript.Echo "Message: File " & strFileName & " has not been modified "
WScript.Echo "Statistic: " & strSize
WScript.Quit( SUCCESS )

End If

'executed if modified date does not match - file must be modified
' uncomment the next line if you would like the state file to update even when it has been modified
WScript.Echo "Message: File " & strFileName & " has been modified "
WScript.Echo "Statistic: " & strSize
WScript.Quit(3)

End If

WScript.Echo "Message: File " & strFileName & " has been baselined."
WScript.Echo "Statistic: " & strSize
WScript.Quit( SUCCESS )

'update the state file

Set objStateFile = objFso.GetFile(strStateFile)
Set objStream = objStateFile.OpenAsTextStream(2, -2)
objStream.WriteLine(strDateModified)
objStream.WriteLine(strSize)
objStream.close

End Function

IT / Software Engineering ConsultantCommented:
It's not clear to me from your last post what change(s) you are in need of to the script.  Could you explain further, and post a sample of the output you would like generated please.

~bp
Author Commented:
Hi,

The output that I was specting is the statistic count, but checking the script running on all 5 minutes cycle the statistic is ok.

Thanks
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.