Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Web Design, HTML And CSS Forums (http://www.go4expert.com/forums/web-design-forum/)
-   -   Searching for a file by today's date. (http://www.go4expert.com/forums/searching-file-todays-date-t7342/)

Tracer 17Nov2007 01:03

Searching for a file by today's date.
 
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 17Nov2007 10:31

Re: Searching for a file by today's date.
 
Have you started writing the script?? Start off something of your own, then we can help you on the way!

Tracer 17Nov2007 22:04

Re: Searching for a file by today's date.
 
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


pradeep 18Nov2007 14:40

Re: Searching for a file by today's date.
 
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 18Nov2007 20:23

Re: Searching for a file by today's date.
 
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 19Nov2007 10:17

Re: Searching for a file by today's date.
 
Then check for the date created!


All times are GMT +5.5. The time now is 15:18.