2012-02-09 38 views
5

Làm cách nào để sửa đổi VBScript này để chỉ trả về tên tệp mới nhất và ngày Sửa đổi lần cuối? Hiện tại, nó trả về mọi thứ được sửa đổi trong 24 giờ qua. Tôi chỉ muốn tìm tập tin mới nhất. Tôi mượn điều này từ StackOverflow, chưa phải là một thuật sĩ VBScript.Sử dụng VBScript để tìm ngày tệp gần đây nhất trong một thư mục

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 

Trả lời

11

bạn đang khá gần với nó:

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 
Các vấn đề liên quan