2012-07-12 53 views
1

目標:運行每天檢查文件夾的VBScript,並報告當天是否沒有文件保存。忽略前幾天存在的文件。VBScript - 文件未創建時的通知

場景:日誌文件在C:\ Temp每天早上3點創建。這就是告訴我們系統執行了一項任務。如果沒有生成日誌文件,則任務崩潰。我編寫了這個程序來檢查當前創建的文件的Temp文件夾,如果它不存在,給我發電子郵件。

解決方案迄今:

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 

問題我有:

我似乎無法換我的頭周圍的方式使用該腳本忽略來自前幾天的文件夾中的文件。

在我的測試中,我有C:\ Temp popuruated,今天修改了一個文件,並在2012年7月10日修改了一個文件。因爲這個場景匹配'then'和'else'語句,所以這兩個都是。

我想我只需要稍微修改一下循環來告訴它 忽略文件沒有過期日期 如果今天沒有文件存在,請發送電子郵件。

任何幫助都會很棒。我似乎無法「看到」答案。

回答

1

你很近。問題在於你在循環查看每一個文件。您只需檢查一個文件是否不存在。我對vbscript並不熟悉,所以您可能需要稍微調整一下,但我所做的是添加變量found並將其初始化爲false。如果您發現過去24小時內創建的文件,請將其設置爲true。一旦您完成了循環,如果它仍然false,沒有文件在過去24小時內

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 
+0

就是這樣!我只需要一個觸發器 - 謝謝你! – 2012-07-12 18:58:29

0

我建議首先從日期檢查移除時

其次既然你說,該文件被修改創建每晚我會檢查DateCreated而不是DateModified。

我修改下面的代碼添加變量點心指明MyDate,然後將其設置爲前一天

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

後來我改了行

if file.DateCreated > myDate then 

看新變量。
使用echo命令運行此工作,正如您所描述的 一樣,並僅通知我今天創建的文件。

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 
0

這個腳本:

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 

輸出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 ... 

輸出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 

上打破/退出循環鬼的做法,儘快膨脹,一個/最新的文件被找到,avoi ds重新計算循環中的檢查日期(基於易失性Now!),並說明不寫入代碼的重要性(例如:Set folder = ...,If(found = false)..)。