【问题标题】:Get the last modified file from a folder with specific extension in vbs从vbs中具有特定扩展名的文件夹中获取最后修改的文件
【发布时间】:2013-07-23 09:29:50
【问题描述】:

我有以下一段代码,我只需要找到扩展名为 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

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

【问题讨论】:

    标签: vbscript


    【解决方案1】:

    先过滤扩展:

      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

    【讨论】:

    • 我怎么会这么笨,错过了这样一个通用的逻辑X-(,非常感谢兄弟!
    猜你喜欢
    • 1970-01-01
    • 2022-07-18
    • 2021-06-08
    • 1970-01-01
    • 2011-03-10
    • 2018-12-23
    • 2019-06-14
    • 1970-01-01
    • 2014-07-30
    相关资源
    最近更新 更多