从vbs中具有特定扩展名的文件夹中获取最后修改的文件

Saq*_*qib 2 vbscript

我有以下一段代码所有我需要的是找到扩展名为PNG的文件和最近的最后修改日期,我能够找到最后修改日期,但如果我将扩展名检查放在文件上它会给出[对象的错误在[某些数字]需要'recentFile']

脚本

For Each objFile in colFiles
    ' Finds the latest modified file in folder
    if (recentFile is nothing) then
        Set recentFile = objFile
        elseif (objFile.DateLastModified > recentFile.DateLastModified) then
            Set recentFile = objFile
    end if
Next
Run Code Online (Sandbox Code Playgroud)

我知道我可以稍后检查扩展名,但问题是,如果有一个最新的文件而不是PNG怎么办?虽然有文件的PNG扩展名但与其他文件相比不是最新的,所以我只需要找到最后修改日期的PNG到最新的PNG文件,请帮助我如何实现它?

Ekk*_*ner 8

首先过滤扩展名:

  Dim oLstPng : Set oLstPng = Nothing
  Dim oFile
  Dim goFS    : Set goFS    = CreateObject("Scripting.FileSystemObject")
  For Each oFile In goFS.GetFolder("..\testdata\17806396").Files
      If "png" = LCase(goFS.GetExtensionName(oFile.Name)) Then
         If oLstPng Is Nothing Then 
            Set oLstPng = oFile ' the first could be the last
         Else
            If oLstPng.DateLastModified < oFile.DateLastModified Then
               Set oLstPng = oFile
            End If
         End If
      End If
  Next
  If oLstPng Is Nothing Then
     WScript.Echo "no .png found"
  Else
     WScript.Echo "found", oLstPng.Name, oLstPng.DateLastModified
  End If
Run Code Online (Sandbox Code Playgroud)

(看看这里)