Link to home
Start Free TrialLog in
Avatar of Headmasters
HeadmastersFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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?
Avatar of Bill Prew
Bill Prew

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
Avatar of Headmasters

ASKER

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.
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
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?
Can you provide a sample of the output you would like?

~bp
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.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Excellent, works just as we needed. Thanks for your help
Welcome, glad that was useful.

~bp