Использование VBScript для поиска самой последней даты файла в одной папке - PullRequest
5 голосов
/ 09 февраля 2012

Как я могу изменить этот 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 Ответ

13 голосов
/ 09 февраля 2012

Следующее реализует GetRecentFileGetRecentFolder) и включает пример использования:

Option Explicit  

Function GetRecentFile(path)
  Dim fso, file
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set GetRecentFile = Nothing
  For Each file in fso.GetFolder(path).Files
    If GetRecentFile is Nothing Then
      Set GetRecentFile = file
    ElseIf file.DateLastModified > GetRecentFile.DateLastModified Then
      Set GetRecentFile = file
    End If
  Next
End Function

Function GetRecentFolder(path)
  Dim fso, folder
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set GetRecentFolder = Nothing
  For Each folder in fso.GetFolder(path).SubFolders
    If GetRecentFolder is Nothing Then
      Set GetRecentFolder = folder
    ElseIf folder.DateLastModified > GetRecentFolder.DateLastModified Then
      Set GetRecentFolder = folder
    End If
  Next
End Function

Dim recentFile
Set recentFile = GetRecentFolder("C:\Temp")
If recentFile is Nothing Then
  WScript.Echo "No recent files found"
Else
  WScript.Echo "Recent file is " & recentFile.Name & " " & recentFile.DateLastModified
End If
...