Monday, February 18, 2013

'VBScript to check file creation date and extension name 
'Script checks for files with particular extension in the specified folder and checks their creation date
'Rahul Patel

option explicit

dim objFSO, objStartFolder, objFolder, colFiles, ObjFile, DFC, count, objMessage
Set objFSO = CreateObject("Scripting.FilesystemObject")

'Change the directory you want to check for the file generation
objStartFolder = "C:\Temp"

Set objFolder = objFSO.GetFolder(objStartFolder)
Wscript.Echo objFolder.path

Set colFiles = objFolder.Files
Set objMessage = CreateObject("CDO.Message")

count = 0

For Each objFile in colFiles
'the FileExtension is CASE SENSITIVE... so make sure the extension check has the correct CASE
If objFSO.GetExtensionName(objFile.Name) = "txt" then
    DFC = objFile.Datecreated
    wscript.echo DFC
    ' change the "D" to "h" and then the < 2 to check on hours..
    IF (DateDiff("D", DFC, Date) < 2 ) then
        count = count + 1
        ' Can comment the filename output below... this was for testing only.
        Wscript.echo objFile.Name
    End IF
END IF

Next

IF count < 1 Then
'edit the text you want in the email sent to whatever you like if the FILES were NOT Generated...
'Wscript.echo "Files not generated"
objMessage.TextBody = "Files were NOT generated."
ELSE
'edit the text you want in the email sent to whatever you like if the FILES were Generated...
'Wscript.echo "Files were generated Fine... "
objMessage.TextBody = "Files were generated....."
END IF


objMessage.Subject = "File Generation check"
'FROM field
objMessage.From = "rahul.patel@met.com.au"
'TO Field
objMessage.To = "rahul.patel@met.com.au"
'objMessage.TextBody = "This is some sample message text."
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'you will need anonymous allowed on the mail server to be able to run this script to send email without authentication. Else
'objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
'Recommend using Basic Auth
'Your UserID on the SMTP server
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "youruserid"
'Your password on the SMTP server
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "yourpassword"

'The server below is for test purpose only. Suggest you get the correct server name.
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mtbexc08v.met.local"
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMessage.Configuration.Fields.Update
objMessage.Send

'just to test the count output.
'wscript.echo Count

No comments:

Post a Comment