Solved Email when files exist over 15min

November 17, 2011 at 14:43:17
Specs: Windows Server 2003
I have a list of directories that get files added to by other users on my network.

I would like a script (triggered by scheduled task) that would detect if there are files in these folders older than 15 minutes, and send me an email.

I am able to run Batch/DOS, Python, Perl, or VBS - I have them all installed on the server. As long as they can be triggered from Scheduled task.

Example:
C:\UsersFTP\JoeS\FromCustomer\ <files dropped here>
C:\UsersFTP\Mark\FromCustomer\ <files dropped here>
C:\UsersFTP\Robbie\FromCustomer\ <files dropped here>
C:\UsersFTP\Tammy\FromCustomer\ <files dropped here>

- Check every 15 mintues if any FILES exist in these directories (along with 50 more).
- It should ignore folders.
- If No, quit.
- If yes, email user@emailaddress.com and notify that files are waiting to be reviewed.
- The scheduled task can run it every 15 minutes.
- I would like it to only send ONE email, no matter how many files are waiting.

Example notification: (the XX are variables)

= = = = =
From: Notifer@youremail.com
To: user@emailaddress.com
Subject: Files in FromCustomer are waiting


You Have files that are waiting since xx:xx:xx PM (over XX minutes).

The files are located in:

C:\UsersFTP\Tammy\FromCustomer\
C:\UsersFTP\Robbie\FromCustomer\

Please process or remove them. Thank you.

= = = = =

I have been looking at doing this in Batch, but I just don't think it is capable... thoughts, help?



See More: Email when files exist over 15min

Report •


✔ Best Answer
November 18, 2011 at 14:22:53
Man, I just do not want to write VBScript today and I have no idea why.

Untested:

Const rootDir     = "C:\UsersFTP"
Const gracePeriod = 5 'In minutes
Const emailTo     = "user@emailaddress.com"
Const emailFrom   = "Notifer@youremail.com"
Const emailSbjct  = "Files in FromCustomer are waiting"
      emailMsgSt  = "You have files older than the cutoff period of " _
                    & gracePeriod & " minutes." & vbNewLine & vbNewLine _
                    & "The files are located in: " & vbNewLine
      emailMsgEd  = vbNewLine & "Please prossess or remove them. Thank you."
Const SMTPServer  = "smtp.some.com"
Const SMTPPort    = 25
Const emailUsr    = "you@some.com"
Const emailPsswd  = "yourEmailPassword"
Const emailSSL    = False

Set fso = CreateObject("Scripting.FileSystemObject")
WScript.Quit Main
Function Main() 'As Integer
  Const magicSubdir = "FromCustomer"
  Set dirList = CreateObject("Scripting.Dictionary")
  cutoff = DateAdd("n", -gracePeriod, Now)
  For Each dir In fso.GetFolder(rootDir).SubFolders
    If fso.FolderExists(dir & "\" & magicSubdir) Then
      If OldFiles(dir.SubFolders(magicSubdir), cutoff) Then _
        dirList(dir) = True
    End If
  Next 'dir
  
  If dirList.Count Then
    For Each key In dirList.Keys
      msg = msg & key & "\" & magicSubdir & vbNewLine
    Next 'key
    SendEmail emailMsgSt & msg & emailMsgEd
  End If
  Main = 0
End Function


Function OldFiles(dir, cutoff) 'As Boolean
  OldFiles = True
  For Each file In dir.Files
    If file.DateCreated < cutoff Then _
      Exit Function 'return True
  Next 'file
  
  For Each subdir In dir.SubFolders
    If OldFiles(subdir, cutoff) Then _
      Exit Function 'return True
  Next 'subdir
  OldFiles = False
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



#1
November 17, 2011 at 16:39:21
It could be done it batch but it would be complicated in batch. You would need a command line email app to accept commands sent from your script. There are freeware SNMP mailer clients available that can do this.

The hardest thing for me in this script would be the condition of 'files and folders older than 15 minutes'. Batch can not add and subtract based on time. Batch is only capable of basic arithmetic compared to other real scripting technologies.

There are some crude batch scripting that could get around this by taking the individual numbers of the time and adding and subtracting based on them but that starts to get complicated and messy. Saying that however there are some great batch scripters in this forum group that may have a answer but for me its a long and messy one.

A batch script could tell you if there are new files in the folders, copy them, move them and email you but the time 15 min condition is a little complicated. Your better off using VBSCRIPT for that one.

I suggest asking in the VBSCRIPT forum for a code snippet on your time condition and calling that from your batch script or using vbscript for the whole project.


Report •

#2
November 17, 2011 at 17:10:34
Thank you for the quick reply - I was coming to that conclusion myself, but was *hoping* there was a way around it.

I simply know very little about programming in VB.
I knew there was email issue with batch.
I knew that minutes are hard to deal with in batch.

I do welcome any further thoughts or options on this, and I will post to A VB forum to see what I can find out.


Report •

#3
November 18, 2011 at 01:21:28
This is not really a job for batch. I'm constantly amazed by the hoops people jump through to get batch scripts to perform tasks that are trivial in other programming languages.

In this case I would use a simple VB or C# program, making use of the FileSystemWatcher class to monitor the folder(s).


Report •

Related Solutions

#4
November 18, 2011 at 13:12:56
Yes, I've come to that conclusion as well, iJack. My intention was not to jump through hoops, it's simply that this server makes use of batchfiles for most of it's maintenance, so i was attempted to keep suit.

I do have a VBS in progress to do this. .. it's just not my native language.


Report •

#5
November 18, 2011 at 14:22:53
✔ Best Answer
Man, I just do not want to write VBScript today and I have no idea why.

Untested:

Const rootDir     = "C:\UsersFTP"
Const gracePeriod = 5 'In minutes
Const emailTo     = "user@emailaddress.com"
Const emailFrom   = "Notifer@youremail.com"
Const emailSbjct  = "Files in FromCustomer are waiting"
      emailMsgSt  = "You have files older than the cutoff period of " _
                    & gracePeriod & " minutes." & vbNewLine & vbNewLine _
                    & "The files are located in: " & vbNewLine
      emailMsgEd  = vbNewLine & "Please prossess or remove them. Thank you."
Const SMTPServer  = "smtp.some.com"
Const SMTPPort    = 25
Const emailUsr    = "you@some.com"
Const emailPsswd  = "yourEmailPassword"
Const emailSSL    = False

Set fso = CreateObject("Scripting.FileSystemObject")
WScript.Quit Main
Function Main() 'As Integer
  Const magicSubdir = "FromCustomer"
  Set dirList = CreateObject("Scripting.Dictionary")
  cutoff = DateAdd("n", -gracePeriod, Now)
  For Each dir In fso.GetFolder(rootDir).SubFolders
    If fso.FolderExists(dir & "\" & magicSubdir) Then
      If OldFiles(dir.SubFolders(magicSubdir), cutoff) Then _
        dirList(dir) = True
    End If
  Next 'dir
  
  If dirList.Count Then
    For Each key In dirList.Keys
      msg = msg & key & "\" & magicSubdir & vbNewLine
    Next 'key
    SendEmail emailMsgSt & msg & emailMsgEd
  End If
  Main = 0
End Function


Function OldFiles(dir, cutoff) 'As Boolean
  OldFiles = True
  For Each file In dir.Files
    If file.DateCreated < cutoff Then _
      Exit Function 'return True
  Next 'file
  
  For Each subdir In dir.SubFolders
    If OldFiles(subdir, cutoff) Then _
      Exit Function 'return True
  Next 'subdir
  OldFiles = False
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 •

#6
November 23, 2011 at 10:40:54
Thanks for the pointers, I was able to create a working prototype:


'------------------- Billie Hawkins  ---------------------
'------------------- Check Files VBS ---------------------



    'Dim ShowFolderList(folderspec)
Set oFSO = CreateObject("Scripting.FileSystemObject")

    Dim folderspec
    Dim fs, f, f1, fc, s
    folderspec = "C:\Users\"
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(folderspec)
    Set fc = f.SubFolders
    For Each f1 in fc
        s = s & f1.name 
        's = s &  vbCrLf
    
    


sPath = "C:\Users\" & f1.name & "\FromCustomer"
'wscript.echo sPath

Set oFolder = oFSO.GetFolder(sPath)
 On Error Resume Next
Set oFiles = oFolder.Files

If oFiles.Count > 0 Then

  ' Enumerate the files in the folder looking for files updated
  ' within the last xx minutes. If such a file is found, exit the loop.

  bolFileIsNewEnough = False  ' init value
  For Each oFile In oFiles
    On Error Resume Next
    dFileModDate = oFile.DateLastModified
    If Err.Number = 0 Then
      If DateDiff("n", dFileModDate, Now) < 15 Then
        bolFileIsNewEnough = True
        Exit For
      End If
    End If
  Next
  On Error Goto 0

  If Not bolFileIsNewEnough Then
          Set objEmail = CreateObject("CDO.Message")
            objEmail.From = "File-Alert@msbinfo.com"
            objEmail.To = "billie.hawkins@email.com"
            objEmail.Subject = "Error: Files sitting over 15 minutes. " 
            objEmail.Textbody = sPath & " has files older than 15 Min." & VBCRLF & " Please check." 


            objEmail.Configuration.Fields.Item _
            ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
            objEmail.Configuration.Fields.Item _
            ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "my.smtp.com"
            objEmail.Configuration.Fields.Item _
            ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
            objEmail.Configuration.Fields.Update
            objEmail.Send
  End if

Else
  'WScript.Echo "Directory is empty"
End If
Next

'--------------------Billie Check Files VBS----------------------


Report •

Ask Question