2

i have the following piece of code all I need is to find the file with extension PNG and most recent last modified date, I am able to find the last modified date, but if I place check of extension over file it gives error of [Object needed 'recentFile' at line [some number]]

SCRIPT

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

I know I can check extension later on but the issue is that what if there is a file which is latest and is not PNG? while there are files with PNG extension but not latest as compare to other files, so I just need to find PNG with last modified date to latest for only PNG files, please help how can I implement it?

4

1 回答 1

8

Filter for the extension first:

  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

(have a look here)

于 2013-07-23T10:19:33.020 回答