VBS脚本,显示在特定日期创建的文件

时间:2016-04-19 15:31:49

标签: date vbscript

我想创建一个脚本,显示在特定位置的特定日期创建的文件。 至于现在我创造了这个:

Set objWMIService = GetObject("winmgmts:" & "!\\" & "." & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery ("Select * from CIM_DataFile where Drive='C:' AND Path='\\' AND CreationDate Like '20071107%' ")
For Each objFile in colFiles
Buffer = Buffer & objFile.FileName & " - " & objFile.CreationDate & vbNewLine 
Next
Wscript.echo Buffer

但我在这一行中遇到错误:“和CreationDate喜欢'20071107%'” 所以它不会像我想象的那样工作 - 在C:\我有很多eula.txt文件创建于2007年11月7日。 我不问完成的代码,但只是为了一个线索。谢谢!

1 个答案:

答案 0 :(得分:0)

WHERE查询的

WQL子句保护或禁止在CIM_DATETIME值中使用通配符。使用SWbemDateTime object,如下所示:

option explicit
On Error GoTo 0
Dim strResult: strResult = Wscript.ScriptName
Dim objWMIService, colFiles, objFile, dTargetDate, dMinDate, dMaxDate, dateTime

Set dateTime = CreateObject("WbemScripting.SWbemDateTime")
dTargetDate = #2007-11-07#          ' date literal in yyyy-mm-dd format

dateTime.SetVarDate( dTargetDate)   ' convert to CIM_DATETIME
dMinDate = datetime               

dateTime.SetVarDate( DateAdd( "d", 1, dTargetDate))
dMaxDate = datetime
strResult = strResult & vbNewLine & dMaxDate


Set objWMIService = GetObject("winmgmts:" & "!\\" & "." & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery ( _
  "Select * from CIM_DataFile where Drive='c:' AND Path='\\'" _
    & " AND CreationDate >= '" & dMinDate & "'" _
    & " AND CreationDate  < '" & dMaxDate & "'" _
    )

If colFiles.Count = 0 Then
  strResult = strResult & vbNewLine & "no files found"
Else
  For Each objFile in colFiles
    strResult = strResult & vbNewLine & objFile.FileName  & " " & objFile.Extension  _
      & " - " & objFile.CreationDate
  Next
End If

Wscript.Echo strResult
Wscript.Quit