Script To Check Files Added in a Folder and Alarms if Files Have Been Added

Hi,
I´m on the task to automate our checklist of critical file transfers, so I´m looking for a vb script to monitor a specific folder that receives critical files some times on a week. The idea of the script is that when a file or files have been added, it can generate an alert such as an output message like WScript.Echo "Message: The following files have been added: + date/time added" and an output like WScript.Quit(1), and if one or more of those files in that folder stay more than 5 minutes it generates an output like WScript.Quit(3) and an output message like WScript.Echo "Message: The following files are on queue more than 5 minutes: files on queue"

Thanks for your help
andresskAsked:
Who is Participating?
 
Bill PrewCommented:
Did you want to change this line:

WScript.Echo vbCrLf & "Count of exceptions found: [" & intCount & "]"

to this:

WScript.Echo vbCrLf & "Statistic:" & intCount

or do you mean you need another line that always shows exactly what you typed, including the 0.  If so then add another line as in:

WScript.Echo vbCrLf & "Count of exceptions found: [" & intCount & "]"
WScript.Echo "Statistic:0"

No, 500 is the max points.

~bp
0
 
prashanthdCommented:
How often will the script be scheduled to run?
0
 
andresskAuthor Commented:
Hi,
Thanks for your reply. It will run every five minutes for different servers
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Bill PrewCommented:
So will the folder normally be empty?  Files are normally processed as soon as they arrive, but this watchdog process is looking for any that are left unprocessed?

~bp
0
 
andresskAuthor Commented:
Yes, the files are processed as soon as they arrive, so the folder normally will be empty and the idea of the script is that we can check when a file arrives and if that file has not been processed
0
 
Bill PrewCommented:
See if this is close to what you want.

' Set base folder to monitor
Const conBaseDir = "X:\EE"

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

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

' Initialize flags
blnFive = False
blnSome = False

' 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) > 5 Then 
      Wscript.Echo "File older than 5 mins: [" & objFile.Name & "], Created at: [" & objFile.DateCreated & "]"
      blnFive = True
   Else
      ' Otherwise just indicate there are new files
      Wscript.Echo "File added to queue: [" & objFile.Name & "], Created at: [" & objFile.DateCreated & "]"
      blnSome = True
   End If
Next

' Return appropriate return code to calling program
If blnFive Then WScript.Quit(3)
If blnSone Then WScript.Quit(1)
WScript.Quit(0)

Open in new window

~bp
0
 
andresskAuthor Commented:
Hi billprew,
Thanks for your help. It´s working and detects the old files and the new ones, is it possible that this script can alert when the folder has been modified? because sometimes, there are files that are added into the folder and stay there only a few seconds, so, between the last check of the script and the new check interval after 5 minutes, maybe it can´t detect changes
Thanks again
0
 
Bill PrewCommented:
Give this a try.  I changed the return codes as follows:

0 = no files found, no data change on folder
1 = no files found, folder was updated
2 = files found, all less than 5 mins old
3 = files found, some greater than 5 mins old

' Set base folder to monitor
Const conBaseDir = "X:\EE"
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

' 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 "File older than 5 mins: [" & objFile.Name & "], Created at: [" & objFile.DateCreated & "]"
      intReturn = 3
   Else
      ' Otherwise just indicate there are new files
      Wscript.Echo "File added to queue: [" & objFile.Name & "], Created at: [" & objFile.DateCreated & "]"
      If intReturn = 0 Then intReturn = 2
   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 "No files found, but queue modified at: [" & objFolder.DateLastModified & "]"
      intReturn = 1
   End If
End If

' Return appropriate return code to calling program
WScript.Quit(intReturn)

Open in new window

~bp
0
 
andresskAuthor Commented:
Hi billprew,

I´ve made some tests and it´s working pretty fine, one last thing is if I can add this line to the script and where: WScript.Echo("Statistic: a numeric value such as 0 or the number of found errors").

Thank you very much.
0
 
Bill PrewCommented:
This should add a summary count.

' Set base folder to monitor
Const conBaseDir = "X:\EE"
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 "File older than 5 mins: [" & objFile.Name & "], Created at: [" & objFile.DateCreated & "]"
      intReturn = 3
      intCount = intCount + 1
   Else
      ' Otherwise just indicate there are new files
      Wscript.Echo "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 "No files found, but queue modified at: [" & objFolder.DateLastModified & "]"
      intReturn = 1
      intCount = intCount + 1
   End If
End If

' Return appropriate return code to calling program
WScript.Echo vbCrLf & "Count of exceptions found: [" & intCount & "]"
WScript.Quit(intReturn)

Open in new window

~bp
0
 
andresskAuthor Commented:
Hi billprew,

It´s working perfect. How can add the line WScript.Echo("Statistic:0") because it´s a requiered field on the software that I´m using to automate the execution of the script?

Thanks a lot. Is it possible to add more than 500 points?
0
 
andresskAuthor Commented:
The first one is ok.
Thanks a lot billprew
0
 
andresskAuthor Commented:
Great
0
 
Bill PrewCommented:
Welcome.

~bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.