0

I need vbscript to iterate files through files and find all files which are created today.

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

I can see there are 40 files in the folder for today but when the script scans though all files, it lists files less than 40. Maybe it is also looking at the time portion.

Can anyone tell me exactly how to use the datediff function so that I can achieve the desired.

I want it to get all files whose DATE portion is today's date portion without any consideration for the time portion.

4

2 回答 2

0

When you create a datetime value without a time portion in VBScript, the time is automatically assumed to be 00:00:00 (see for instance the return value of TimeValue(Date)). Because of this DateDiff() compares the "last modified" timestamp of the file with the current date at 00:00:00 and returns a value greater than 1 (or less than -1) when the difference exceeds ±24 hours.

For comparing just the date parts of two timestamps use the FormatDateTime() function:

today = FormatDateTime(Date, vbShortDate)
If FormatDateTime(RFile.DateLastModified, vbShortDate) = today Then
  '...
End If
于 2012-11-04T21:10:01.990 回答
0

Your better off just enumarating the files with WMI manupulating the date to just compare the day month and year attributes ignoring the time stamp.

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
于 2012-11-05T19:57:14.157 回答