1

目标:运行一个每天检查文件夹的 VBScript,并报告当天是否没有文件保存到该文件夹​​。忽略前几天存在的文件。

场景:每天凌晨 3 点在 C:\Temp 中创建一个日志文件。这就是告诉我们系统执行了一项任务的原因。如果未生成日志文件,则任务崩溃。我写这篇文章是为了检查 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 中填充了今天修改的文件,以及 2012 年 7 月 10 日修改的文件。因为这个场景同时匹配 'then' 和 'else' 语句,所以它两者都做。

我想我只需要对循环稍作修改即可告诉它 - 忽略不是“今天”日期的文件 - 如果今天不存在文件,请发送电子邮件。

任何帮助都是极好的。我似乎无法“看到”答案。

4

3 回答 3

3

你很近。问题是您正在循环并检查每个文件。您只需要检查一个文件是否不存在。我对 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
于 2012-07-12T18:22:10.397 回答
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

通过在找到/最新文件后立即中断/退出循环来扩展 Ghost 的方法,避免在循环中重新计算检查日期(基于 volatile Now!),并展示代码的重要性你不写(例如:Set folder = ..., If (found = false) ..)。

于 2012-07-12T19:03:32.167 回答
0

我建议首先从日期检查中删除时间

第二,既然你说文件是每晚创建的,我会检查 DateCreated 而不是 DateModified。

我在下面修改了您的代码以添加变量 Dim 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 
于 2012-07-12T18:41:58.810 回答