Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
' Copyright 2013 Typomedia Foundation. All rights reserved.
' Released under GPL version 3.
'
' VBS New Files Mailer v1.1
' Send a mail if files of a specific dir are newer than x minutes
Const intMin = 60 'Minutes
Set oArgs = WScript.Arguments
Set fso = CreateObject("Scripting.FileSystemObject")
Message = GetFiles(oArgs.Item(1))
If Message <> "" then 'if Message is not empty
SendMail(Message)
'MsgBox(Message)
End if
Function SendMail(Message)
Set objEmail = CreateObject("CDO.Message")
objEmail.From = """Windows Server"" <server@domain.tld>"
objEmail.To = oArgs.Item(0)
'objEmail.CC = "someone@domain.tld"
objEmail.Subject = "New Files"
objEmail.HTMLBody = "<html><body><pre>" & Message & "</pre></body></html>"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.provider.tld"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = "server@domain.tld"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "<password>"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
objEmail.Configuration.Fields.Update
objEmail.Send
End Function
Function GetFiles(strPath)
On Error Resume Next
Set objFolder = fso.GetFolder(strPath)
'Get all Files from folder
For Each objFile In objFolder.Files
fileCreateDate = objFile.DateCreated
'fileModDate = objFile.DateLastModified
'fileAccessDate = objFile.DateLastAccessed
If Err.Number = 0 Then
If DateDiff("n", fileCreateDate, Now) < intMin Then '< newer than
strFiles = strFiles & objFile.Path & vbNewLine
End if
End if
Next
'Get all subfolders
For Each objFolder In objFolder.SubFolders
'Get all Files from subfolder
strFiles = strFiles & GetFiles(objFolder.Path)
Next
GetFiles = strFiles
End Function
@typomedia
Copy link
Author

typomedia commented Apr 29, 2014

USAGE: newfiles.vbs somebody@mail.com "\SERVER\Incoming"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment