【问题标题】:VBScript - Notification when file not created todayVBScript - 今天未创建文件时的通知
【发布时间】:2012-07-12 18:12:29
【问题描述】:

目标:运行一个每天检查文件夹的 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' 语句,所以它两者都做。

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

任何帮助都会很棒。我似乎无法“看到”答案。

【问题讨论】:

    标签: file if-statement vbscript exists last-modified


    【解决方案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
    

    【讨论】:

      【解决方案2】:

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

      第二,既然你说文件是每晚创建的,我会检查 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 
      

      【讨论】:

        【解决方案3】:

        这个脚本:

          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) ..)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-10-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多