1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Searching for a file by today's date.

Discussion in 'Web Design, HTML And CSS' started by Tracer, Nov 16, 2007.

  1. Tracer

    Tracer New Member

    Joined:
    Nov 16, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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?
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    Have you started writing the script?? Start off something of your own, then we can help you on the way!
     
  3. Tracer

    Tracer New Member

    Joined:
    Nov 16, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: Nov 18, 2007
  4. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    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.
     
  5. Tracer

    Tracer New Member

    Joined:
    Nov 16, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  6. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    Then check for the date created!
     

Share This Page