Searching for a file by today's date.

Tracer's Avatar, Join Date: Nov 2007
Newbie Member
I have no idea what I am doing.

I need to create a vbscript that will search a specific directory for a file with the current date, and if it exists then send it as an attachment via email, and if not, send a warning email.

The script will run everyday and search the same directory everytime for the current date's file.

Help?
pradeep's Avatar, Join Date: Apr 2005
Team Leader
Have you started writing the script?? Start off something of your own, then we can help you on the way!
Tracer's Avatar, Join Date: Nov 2007
Newbie Member
This is the script I have so far. I need to figure out now how to grab the name of the most recent file in the specified dir that is returned and send it as an attachment. Is there a way of wild carding that?
Code:
sPath = "c:\mssql\reports"


Set oFSO = CreateObject("Scripting.FileSystemObject")

sNewestFile = GetNewestFile(sPath)

If sNewestFile <> "" Then

   WScript.Echo "Newest file is " & sNewestFile

   dFileModDate = oFSO.GetFile(sNewestFile).DateLastModified
   If DateDiff("h", dFileModDate, Now) > 24 Then
      Set objMessage = CreateObject("CDO.Message") 
      objMessage.Subject = "Warning - No new report available" 
      objMessage.From = "firstname_lastname@comany.com"  
      objMessage.To = "firstname_lastname@company.com" 
      objMessage.TextBody = "Warning - No new report file was found in reports dir.  please investigate."

      objMessage.Configuration.Fields.Item _
      ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 

      objMessage.Configuration.Fields.Item _
      ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "exchange_server_name"

      objMessage.Configuration.Fields.Item _
      ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 

      objMessage.Configuration.Fields.Update

      objMessage.Send
      End If

Else
   Set objMessage = CreateObject("CDO.Message") 
      objMessage.Subject = "Warning - report directory is empty" 
      objMessage.From = "firstname_lastname@comany.com"  
      objMessage.To = "firstname_lastname@company.com" 
      objMessage.TextBody = "Warning - Reports directory is empty, please investigate."

      objMessage.Configuration.Fields.Item _
      ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 

      objMessage.Configuration.Fields.Item _
      ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "exchange_server_name"

      objMessage.Configuration.Fields.Item _
      ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 

      objMessage.Configuration.Fields.Update

      objMessage.Send
End If


Function GetNewestFile(ByVal sPath)

   sNewestFile = Null   ' init value

   Set oFSO = CreateObject("Scripting.FileSystemObject")
   Set oFolder = oFSO.GetFolder(sPath)
   Set oFiles = oFolder.Files

   ' enumerate the files in the folder, finding the newest file
   For Each oFile In oFiles
     On Error Resume Next
     If IsNull(sNewestFile) Then
       sNewestFile = oFile.Path
       dPrevDate = oFile.DateLastModified
     Elseif dPrevDate < oFile.DateLastModified Then
       sNewestFile = oFile.Path
     End If
     On Error Goto 0
   Next

   If IsNull(sNewestFile) Then sNewestFile = ""

   GetNewestFile = sNewestFile

End Function

Last edited by shabbir; 18Nov2007 at 11:00.. Reason: Code block
pradeep's Avatar, Join Date: Apr 2005
Team Leader
Iterate through all the files in the directory and match in to the date created! Or if you have a fixed filename format like its named like "16112007_file.txt" then you can directly check whether it exists or not.
Tracer's Avatar, Join Date: Nov 2007
Newbie Member
That's why I need to search by current date, the file name is not ever the same. It a generated daily report. It will always be in the same directory, and it will always be a text file, but the name of the file changes.
pradeep's Avatar, Join Date: Apr 2005
Team Leader
Then check for the date created!