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


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."
'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....."

objMessage.Subject = "File Generation check"
'FROM field
objMessage.From = ""
'TO Field
objMessage.To = ""
'objMessage.TextBody = "This is some sample message text."
objMessage.Configuration.Fields.Item ("") = 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 ("") = 1
'Recommend using Basic Auth
'Your UserID on the SMTP server
objMessage.Configuration.Fields.Item ("") = "youruserid"
'Your password on the SMTP server
objMessage.Configuration.Fields.Item ("") = "yourpassword"

'The server below is for test purpose only. Suggest you get the correct server name.
objMessage.Configuration.Fields.Item ("") = "mtbexc08v.met.local"
objMessage.Configuration.Fields.Item ("") = 25

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

No comments:

Post a Comment