【问题标题】:DateDiff vbscript to find current date filesDateDiff vbscript 查找当前日期文件
【发布时间】:2012-11-04 18:26:26
【问题描述】:

我需要 vbscript 来遍历文件并找到今天创建的所有文件。

DateDiff("d",RFile.DateLastModified ,Date)=0

我可以看到今天文件夹中有 40 个文件,但是当脚本扫描所有文件时,它列出的文件少于 40 个。也许它也在查看时间部分。

谁能告诉我具体如何使用datediff 函数,以便我达到预期的效果。

我希望它获取 DATE 部分是今天日期部分的所有文件,而不考虑时间部分。

【问题讨论】:

    标签: vbscript


    【解决方案1】:

    当您在 VBScript 中创建不带时间部分的日期时间值时,时间会自动假定为 00:00:00(例如,请参见 TimeValue(Date) 的返回值)。因此,DateDiff() 将文件的“最后修改”时间戳与 00:00:00 的当前日期进行比较,并在差异超过 ±24 小时时返回大于 1(或小于 -1)的值。

    要仅比较两个时间戳的日期部分,请使用 FormatDateTime() 函数:

    today = FormatDateTime(Date, vbShortDate)
    If FormatDateTime(RFile.DateLastModified, vbShortDate) = today Then
      '...
    End If
    

    【讨论】:

    • 谢谢,您能想到一个场景,即 DateDiff("d",RFile.DateLastModified ,Date) 不等于零,而我可以手动看到所有文件今天都具有 DateModefied 吗?
    【解决方案2】:

    你最好只用 WMI 枚举文件来操纵日期,只比较日月和年属性而忽略时间戳。

    strComputer = "."
    
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    
    Set colFiles = _
        objWMIService.ExecQuery("Select * From CIM_DataFile Where Drive = 'E:' and Path = '\\Test\\'")
    
    For Each objFile in colFiles
        dtCreationDate = WMIDateStringToDate(objFile.CreationDate)
        If Day(Now)&Month(Now)&Year(Now) = Day(dtCreationDate)&Month(dtCreationDate)&Year(dtCreationDate) Then
          WScript.Echo objFile.Name
        End If
    Next
    
    Function WMIDateStringToDate(sCreatoionDate)
        WMIDateStringToDate = CDate(Mid(sCreatoionDate, 7, 2) & "/" & _
        Mid(sCreatoionDate, 5, 2) & "/" & Left(sCreatoionDate, 4) _
        & " " & Mid (sCreatoionDate, 9, 2) & ":" & _
        Mid(sCreatoionDate, 11, 2) & ":" & Mid(sCreatoionDate, 13, 2))
    End Function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-01
      相关资源
      最近更新 更多