2013-07-23 27 views
2

特定擴展名的文件夾中的最後一個修改後的文件我有以下的代碼,我需要一塊是找到擴展PNG和最近的最後修改日期的文件,我能找到的最後修改日期,但如果我把延伸檢查了文件時,它給人的錯誤[需要的對象「recentFile」在行[一定數量]獲得來自與VBS

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 

我知道我可以在以後檢查擴展,但問題是,如果有一個文件是最新的,而不是PNG?同時也有與PNG擴展名的文件,但不是最新作比較其他的文件,所以我只需要找到PNG與最後修改日期最新僅供PNG文件,請幫助我如何能實現呢?

回答

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 

(看看here

+0

怎麼可能是我這麼愚蠢錯過這樣一個通用邏輯X-(,非常感謝兄弟! – Saqib