问题描述
我有以下代码,我只需要找到扩展名为 PNG 的文件和最近的最后修改日期,我就能找到最后修改的日期,但是如果我检查文件的扩展名,它会出错的 [对象需要 'recentFile' 在行 [某个数字]]
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]]
脚本
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,请帮助我如何实现它?
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?
推荐答案
先过滤扩展:
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
(查看此处)
这篇关于从 vbs 中具有特定扩展名的文件夹中获取最后修改的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!