1

Goal: To run a VBScript that checks a folder daily, and reports if no files were saved to it that day. Ignore the files that exist from previous days.

Scenario: A logfile is created everyday in C:\Temp at 3am. This is what tells us that the system performed a task. If a log file isn't generated, then the task crashed. I wrote this to check the Temp folder for a file created today, and to email me if it doesn't exist.

Solution thus far:

option explicit 
dim fileSystem, folder, file 
dim path  
path = "C:\Temp" 

Set fileSystem = CreateObject("Scripting.FileSystemObject") 
Set folder = fileSystem.GetFolder(path) 

for each file in folder.Files     
  if file.DateLastModified > dateadd("h", -24, Now) then 
'WScript.Echo file.Name & " last modified at " & file.DateLastModified 
else 
SendEmail
'WScript.Echo "this should have sent an email."
  end if 
next 

Function SendEmail() 
 'Send Email notification function here (this part works already)
End Function

Issue I am having:

I can't seem to wrap my head around a way to have the script ignore files in the folder from previous days.

In my test, I have C:\Temp populuated with a file modified today, and a file modified on 7/10/12. Because this scenario matches both the 'then' and the 'else' statement, it's doing both.

I think I just need a slight modification on the loop to tell it - Ignore files not dated 'today' - Send an email if no files exist today.

Any help would be awesome. I just can't seem to 'see' the answer.

4

3 に答える 3

3

You're close. The problem is is you were looping through and checking every single file. You need to only check if one file doesn't exist. I'm not that familiar with vbscript, so you may need to tweak this a bit, but what I did is add a variable found and initialized it to false. If you find a file created in past 24 hours, set it to true. once you're done looping, if it's still false, no files were modified in past 24 hours

option explicit 
dim fileSystem, folder, file 
dim path  
Dim found
found = false
path = "C:\Temp" 

Set fileSystem = CreateObject("Scripting.FileSystemObject") 
Set folder = fileSystem.GetFolder(path) 

for each file in folder.Files     
  if file.DateLastModified > dateadd("h", -24, Now) then 
    found = true
  end if 
next 
if (found = false) then
  SendEmail
End If


Function SendEmail() 
 'Send Email notification function here (this part works already)
End Function
于 2012-07-12T18:22:10.397 に答える
0

I would suggest first removing the time from the date check

Second Since you stated that the file is created each night I would check the DateCreated and not DateModified.

I modified your code below to add a variable Dim myDate and then set it to the previous day

Dim myDate
myDate =  dateadd("d", -1, FormatDateTime(Now, 2))

I then changed the line

if file.DateCreated > myDate then 

to look at the new variable.
Running this with the echo command worked as you described and notified my of only the file that was created today.

option explicit
dim fileSystem, folder, file  
dim path   
path = "C:\Temp"   
Set fileSystem = CreateObject("Scripting.FileSystemObject")  
Dim myDate
myDate =  dateadd("d", -1, FormatDateTime(Now, 2))
Set folder = fileSystem.GetFolder(path)   
for each file in folder.Files
    if file.DateCreated > myDate then  
        'WScript.Echo file.Name & " last modified at " & file.DateCreated   
        SendEmail 
    'WScript.Echo "this should have sent an email."   
    end if  
next

Function SendEmail()   
'Send Email notification function here (this part works already) 
End Function 
于 2012-07-12T18:41:58.810 に答える
0

This script:

  Option Explicit

  ' config data, fixed
  Const csPATH = "..\data"

  ' config data, computed, show use of DateValue() to cut off time
  Dim dtCheck : dtCheck = DateValue(DateAdd("d", 0, Now))
  WScript.Echo "dtCheck:", dtCheck

  Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")

  Dim bFound : bFound = False ' assume no up-to-date file found
  Dim oFile
  For Each oFile In oFS.GetFolder(csPATH).Files
      WScript.Echo "Check:", DateValue(oFile.DateLastModified), oFile.Name
      If DateValue(oFile.DateLastModified) = dtCheck Then
         WScript.Echo "Found:", DateValue(oFile.DateLastModified), oFile.Name
         WScript.Echo "no need for further loopings"
         bFound = True
         Exit For
      End If
  Next
  If Not bFound Then
     WScript.Echo "Sending email ..."
  End If

output 1:

dtCheck: 12.07.2012
Check: 11.07.2012 11434579.kpf
Check: 11.07.2012 11434579.notes
Check: 11.07.2012 11434579-UE15.prj
Sending email ...

output 2:

dtCheck: 12.07.2012
Check: 11.07.2012 11434579.kpf
Check: 11.07.2012 11434579.notes
Check: 11.07.2012 11434579-UE15.prj
Check: 12.07.2012 11458011.notes
Found: 12.07.2012 11458011.notes
no need for further loopings

expands on Ghost's approach by breaking/exiting the loop as soon as a/the up-to-date file is found, avoids re-computings of the check date (based on volatile Now!) in the loop, and demonstrates the importance of code you don't write (e.g.: Set folder = ..., If (found = false) ..).

于 2012-07-12T19:03:32.167 に答える