by N.K.Velu (2 Submissions)
Category: Files/File Controls/Input/Output
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Sat 9th November 2002
Date Added: Mon 8th February 2021
Rating: (1 Votes)
To find the last modified file in a local or remote folder. This will be useful for tailing log files. Usually several .log files will be
'You need to reference Microsoft Scripting Runtime (scrrun.dll) from Project -> Refernces
'If you don't have scrrun.dll in your C:\winnt\system32 folder get it from Microsoft.
'If you install Windows Script from Microsoft, the dll will be automatically installed
'Get it from http://msdn.microsoft.com/downloads/default.asp?URL=/downloads/sample.asp?url=/msdn-files/027/001/733/msdncompositedoc.xml
'To test pass an existing local or remote folder name like FileName("C:\Utility") or FileName("\\servername\C$\Utility"). You could keep the
'code as a function in an application or put inside a class module and make it as a dll
Dim FileSys As FileSystemObject
Dim File As File
Dim Files As Files
Dim Drive As Drive
Dim allDates() As Date
'If a folder name is not passed to the function, exit the function
If folderName = "" Then Exit Function
Set FileSys = New FileSystemObject
'If a wrong folder name is passed, then also exit
If Not FileSys.FolderExists(folderName) Then
Set FileSys = Nothing
Exit Function
End If
'Get all the files within the specified folder
Set Files = FileSys.GetFolder(folderName).Files
Dim n As Integer
n = -1
ReDim allDates(Files.Count - 1)
For Each File In Files
n = n + 1
'Put the lastmodified date of all the files in to the array
allDates(n) = File.DateLastModified
Next
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim Temp As Variant
k = UBound(allDates)
'Sort the array. The last element will have the latest date
For i = 0 To k - 1
For j = i + 1 To k
If allDates(i) > allDates(j) Then
Temp = allDates(i)
allDates(i) = allDates(j)
allDates(j) = Temp
End If
Next j
Next i
For Each File In Files
'Get the file name which has the latest lastmodified date
If File.DateLastModified = allDates(UBound(allDates)) Then
FileName = File.Name
Exit For
End If
Next
'All done. Free the memory
If Not IsNull(FileSys) Then
Set FileSys = Nothing
End If
If Not IsNull(Files) Then
Set Files = Nothing
End If
End Function
No comments have been posted about To find the last modified file in a local or remote folder. This will be useful for tailing log fil. Why not be the first to post a comment about To find the last modified file in a local or remote folder. This will be useful for tailing log fil.