Script to Show Date Modified for Files

We have a system whereby cab files are copied from remote sites to a server in our office.

I wonder if there is a way for a script to run each day to notify me by email the Date Modified of certain files in certain folders. This way I can then check to make sure that the files have been copied across from the remote sites properly and investigate ones that have not copied.

Is there such a powershell/batch file?
LVL 1
HeadmastersAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
Yes, this could be done.  Would need more details.  Will the script run on the server where the files get copied to?  What files need to be check, what folders do they reside in, what criteria should be used?  Do you have an SMTP: mail server that can be used for sending out the email?  Does it need authentication to send, or no?  Should an email only be sent if the files are not okay, or do you want an email every day either way?

~bp
HeadmastersAuthor Commented:
The script would run on the server where the files get copied to. It would be all files within a folder.

ie. C:\Backups\backup.cab for example

We do have an internal SMTP server for sending out the email and should be sent everyday either way. No authentication is needed as it is internal use.
Bill PrewIT / Software Engineering ConsultantCommented:
Okay, here is a VBS script that should be a good start.  Update the constants and do some testing there and see how it goes.

' Define base folder to scan
Const strFolder = "C:\Temp"

' Define constants for email
Const cSmtpServer = "yourhost.foo.com"
Const cToEmail = "to.email@foo.com"
Const cFromEmail = "from.email@foo.com"

' Set up filesystem object for usage
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Make sure base folder exists
If Not objFSO.FolderExists(strFolder) Then
   SendEmail cSmtpServer, cToEmail, cFromEmail, "Daily server CAB file report", "Missing base folder: " & strFolder & vbCrLf
   WScript.Quit
End If

' Build report of files in folder
strReport = CreateReport(objFSO.GetFolder(strFolder))

' Send report in email
SendEmail cSmtpServer, cToEmail, cFromEmail, "Daily server CAB file report", strReport


' List files
Function CreateReport(objFolder)
   strTemp = "Report Time: " & Now & vbCrLf
   For Each objFile In objFolder.Files
      strTemp = strTemp & objFile.DateLastModified & " - " & objFile.Name & vbCrLf
   Next
   CreateReport = strTemp
End Function

Sub SendEmail(strSmtpServer, strToEmail, strFromEmail, strSubject, strBody)
   ' CDO Constants needed to send email
   Const cCdoSendUsingPickup = 1   'Send message using the local SMTP service pickup directory.
   Const cCdoSendUsingPort = 2     'Send the message using the network (SMTP over the network).
   Const cCdoAnonymous = 0         'Do not authenticate
   Const cCdoBasic = 1             'basic (clear-text) authentication
   Const cCdoNTLM = 2              'NTLM
   Const cCdoSendUsingMethod        = "http://schemas.microsoft.com/cdo/configuration/sendusing"
   Const cCdoSMTPServer             = "http://schemas.microsoft.com/cdo/configuration/smtpserver"
   Const cCdoSMTPServerPort         = "http://schemas.microsoft.com/cdo/configuration/smtpserverport"
   Const cCdoSMTPConnectionTimeout  = "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"
   Const cCdoSMTPAuthenticate       = "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
   Const cCdoSendUserName           = "http://schemas.microsoft.com/cdo/configuration/sendusername"
   Const cCdoSendPassword           = "http://schemas.microsoft.com/cdo/configuration/sendpassword"
   Const cCdoSmtpUseSsl             = "http://schemas.microsoft.com/cdo/configuration/smtpusessl"

   ' Get a handle to the config object and it's fields
   Set objConfig = CreateObject("CDO.Configuration")

   ' Set config fields we care about
   With objConfig.Fields
   	.Item(cCdoSendUsingMethod)       = cCdoSendUsingPort
   	.Item(cCdoSMTPServer)            = strSmtpServer
   	.Item(cCdoSMTPServerPort)        = 25
   	.Item(cCdoSMTPConnectionTimeout) = 10
   	.Item(cCdoSMTPAuthenticate)      = cCdoAnonymous
   	.Update
   End With

   Set objMessage = CreateObject("CDO.Message")
   Set objMessage.Configuration = objConfig

   With objMessage
   	.To       = strToEmail
   	.From     = strFromEmail
      .Subject  = strSubject
      .Textbody = strBody
      .Send
   End With

   Set objMessage = Nothing
   Set objConfig = Nothing
End Sub

Open in new window

~bp
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

HeadmastersAuthor Commented:
Just what we are after. Is there a way to change it to scan multiple folders and list the modify times for each folder

C:\TEMP\Backup 1\
C:\TEMP\Backup 2\

Then on the report state which folder each date stamped object is in?
Bill PrewIT / Software Engineering ConsultantCommented:
Can you provide a sample of the output you would like?

~bp
HeadmastersAuthor Commented:
Report Time: 15/10/2015 12:09:08
14/10/2015 22:00:36 - C:\Temp\Backup 1\Server 1.cab
14/10/2015 22:00:36 - C:\Temp\Backup 2\Server 2.cab
13/10/2015 22:00:36 - C:\Temp\Backup 3\Server 3.cab

We can then look at the above and go there's an issue with Server 3.cab as its not up to date by a day.

I have got the script to do the above but I need it to scan multiple folders in one script rather than using the same script for multiple folder locations.
Bill PrewIT / Software Engineering ConsultantCommented:
Okay, give this a try.

' Define base folder to scan
arrFolders = Array("C:\Temp\Backup 1", "C:\Temp\Backup 2", "C:\Temp\Backup 3")

' Define constants for email
Const cSmtpServer = "yourhost.foo.com"
Const cToEmail = "to.email@foo.com"
Const cFromEmail = "from.email@foo.com"

' Set up filesystem object for usage
Set objFSO = CreateObject("Scripting.FileSystemObject")

strReport = "Report Time: " & Now & vbCrLf
strReport = strReport & "==================================" & vbCrLf

' Process all folders specified
For Each strFolder In arrFolders

   ' Make sure base folder exists
   If objFSO.FolderExists(strFolder) Then
      ' Build report of files in folder
      strReport = strReport & CreateReport(objFSO.GetFolder(strFolder))
   Else
      strReport = strReport & "-----------------------------------------------------------" & vbCrLf
      strReport = strReport & "*WARNING* Missing base folder: " & strFolder & vbCrLf
      strReport = strReport & "-----------------------------------------------------------" & vbCrLf
   End If

Next

' Send report in email
SendEmail cSmtpServer, cToEmail, cFromEmail, "Daily server CAB file report", strReport


' List files
Function CreateReport(objFolder)
   strTemp = ""
   For Each objFile In objFolder.Files
      strTemp = strTemp & objFile.DateLastModified & " - " & objFile.Path & vbCrLf
   Next
   CreateReport = strTemp
End Function

Sub SendEmail(strSmtpServer, strToEmail, strFromEmail, strSubject, strBody)
   ' CDO Constants needed to send email
   Const cCdoSendUsingPickup = 1   'Send message using the local SMTP service pickup directory.
   Const cCdoSendUsingPort = 2     'Send the message using the network (SMTP over the network).
   Const cCdoAnonymous = 0         'Do not authenticate
   Const cCdoBasic = 1             'basic (clear-text) authentication
   Const cCdoNTLM = 2              'NTLM
   Const cCdoSendUsingMethod        = "http://schemas.microsoft.com/cdo/configuration/sendusing"
   Const cCdoSMTPServer             = "http://schemas.microsoft.com/cdo/configuration/smtpserver"
   Const cCdoSMTPServerPort         = "http://schemas.microsoft.com/cdo/configuration/smtpserverport"
   Const cCdoSMTPConnectionTimeout  = "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"
   Const cCdoSMTPAuthenticate       = "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
   Const cCdoSendUserName           = "http://schemas.microsoft.com/cdo/configuration/sendusername"
   Const cCdoSendPassword           = "http://schemas.microsoft.com/cdo/configuration/sendpassword"
   Const cCdoSmtpUseSsl             = "http://schemas.microsoft.com/cdo/configuration/smtpusessl"

   ' Get a handle to the config object and it's fields
   Set objConfig = CreateObject("CDO.Configuration")

   ' Set config fields we care about
   With objConfig.Fields
   	.Item(cCdoSendUsingMethod)       = cCdoSendUsingPort
   	.Item(cCdoSMTPServer)            = strSmtpServer
   	.Item(cCdoSMTPServerPort)        = 25
   	.Item(cCdoSMTPConnectionTimeout) = 10
   	.Item(cCdoSMTPAuthenticate)      = cCdoAnonymous
   	.Update
   End With

   Set objMessage = CreateObject("CDO.Message")
   Set objMessage.Configuration = objConfig

   With objMessage
   	.To       = strToEmail
   	.From     = strFromEmail
      .Subject  = strSubject
      .Textbody = strBody
      .Send
   End With

   Set objMessage = Nothing
   Set objConfig = Nothing
End Sub

Open in new window

~bp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HeadmastersAuthor Commented:
Excellent, works just as we needed. Thanks for your help
Bill PrewIT / Software Engineering ConsultantCommented:
Welcome, glad that was useful.

~bp
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.