首页 文章

使用VBScript在单个文件夹中查找最近的文件日期

提问于
浏览
5

如何修改此VBScript以仅返回最新文件的名称和上次修改日期?目前它返回过去24小时内修改的任何内容 . 我想只查找最新的文件 . 我是从StackOverflow借来的,还不是VBScript向导 .

option explicit  
dim fileSystem, folder, file
dim path   
path = "C:\test"  
Set fileSystem = CreateObject("Scripting.FileSystemObject") 
Set folder = fileSystem.GetFolder(path) 
for each file in folder.Files         
if file.DateLastModified > dateadd("h", -24, Now) then         
'whatever you want to do to process'         
WScript.Echo file.Name & " last modified at " & file.DateLastModified     
end if
next

1 回答

  • 12

    你非常接近它:

    Option Explicit  
    Dim fso, path, file, recentDate, recentFile
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set recentFile = Nothing
    For Each file in fso.GetFolder("C:\Temp").Files
      If (recentFile is Nothing) Then
        Set recentFile = file
      ElseIf (file.DateLastModified > recentFile.DateLastModified) Then
        Set recentFile = file
      End If
    Next
    
    If recentFile is Nothing Then
      WScript.Echo "no recent files"
    Else
      WScript.Echo "Recent file is " & recentFile.Name & " " & recentFile.DateLastModified
    End If
    

相关问题