Script to monitor number of files in folder

January 11, 2011 at 14:20:13
Specs: Windows XP
I need either a batch script or vb script to monitor every 5 min a source Windows folder for more than 2 files. If 1 file, leave it alone. if 2 or more, copy them out to a TEMP folder and send out an alert email

See More: Script to monitor number of files in folder

Report •


#1
January 13, 2011 at 08:45:19
more than 2 files
2 or more
There's a contradiction here. Are you looking for > 2 or >= 2?

How To Ask Questions The Smart Way


Report •

#2
February 24, 2011 at 16:25:55
>=2 please

Report •

#3
March 1, 2011 at 03:50:59
Wow, that was a delayed reply.

This script terminates after it sends the email. If you don't want that, you'll have to modify the script accordingly.

'Modify these lines as required
Const dirPath    = "C:\someDir"
    pollInterval = 60000 * 5 'value in miliseconds (5 minute wait)
Const emailTo    = "admin@some.com"
Const emailFrom  = "you@some.com"
Const emailSbjct = "Monitor email"
Const SMTPServer = "smtp.some.com"
Const SMTPPort   = 25
Const emailUsr   = "you@some.com"
Const emailPsswd = "yourEmailPassword"
Const emailSSL   = False


WScript.Quit Main
Function Main()
  Set wmi = GetObject("winmgmts:")
  Do
    Wscript.Sleep pollInterval
    Set fileList = wmi.ExecQuery( _
        "ASSOCIATORS OF {Win32_Directory.Name='" & dirPath & "'} WHERE " _
        & "ResultClass = CIM_DataFile")
  Loop Until fileList.Count >= 2
  SendEmail dirPath & " -> " & fileList.Count & " file(s)"
End Function

Sub SendEmail(sMsg)
  Const cfg = "http://schemas.microsoft.com/cdo/configuration/"
  With CreateObject("CDO.Message")
    .From                                  = emailFrom
    .To                                    = emailTo
    .Subject                               = emailSbjct
    .TextBody                              = sMsg
    With .Configuration.Fields
      .Item(cfg & "sendusing")             = 2
      .Item(cfg & "smtpserver")            = SMTPServer
      .Item(cfg & "smtpserverport")        = SMTPPort
      .Item(cfg & "smtpconnectiontimeout") = 60
      .Item(cfg & "smtpauthenticate")      = 1
      .Item(cfg & "smtpusessl")            = emailSSL
      .Item(cfg & "sendusername")          = emailUsr
      .Item(cfg & "sendpassword")          = emailPsswd
      .Update
    End With
    .Send
  End With
End Sub

How To Ask Questions The Smart Way


Report •

Related Solutions


Ask Question