-
Does someone have a readymade procedure for sorting out
date time?
I have to find out which is the latest file added to the directory(which is continously getting files into it).
So I need to compare the DateCreated of each of the files at any time to find out the latest entry
-
<?>
'Imagine there are easier ways but this works.
Code:
Option Explicit
Sub SortMeOut(iArray As Variant)
'sort the array
Dim lLoop1 As Long
Dim lLoop2 As Long
Dim lTemp As String
For lLoop1 = UBound(iArray) To LBound(iArray) Step -1
For lLoop2 = LBound(iArray) + 1 To lLoop1
If iArray(lLoop2 - 1) > iArray(lLoop2) Then
lTemp = iArray(lLoop2 - 1)
iArray(lLoop2 - 1) = iArray(lLoop2)
iArray(lLoop2) = lTemp
End If
Next lLoop2
Next lLoop1
End Sub
'uses List1 (listbox) and Command1 (command button)
'file information using File Scripting Object (fso)
'date created.
Private Sub Form_Load()
List1.Clear
Dim stFile As String
Dim stDir As String
Dim fso As Object
Dim myArrDate()
Dim myArrFile()
Dim i As Integer
Dim temp As String
Dim mylen As Integer
Set fso = CreateObject("Scripting.FileSystemObject")
stDir = "C:\My Documents\"
stFile = Dir$(stDir & "*.*")
Do While stFile <> ""
'if you want to access each file you must use the dir
'and the file
stFile = stDir & stFile
'store info in array for sorting purposes
ReDim Preserve myArrDate(i)
temp = Right(Format(fso.GetFile(stFile).DateCreated, "YYYMMDD"), 8)
myArrDate(i) = temp & stFile & Format(fso.GetFile(stFile).DateCreated, "YYYYMMDD")
i = i + 1
stFile = Dir
Loop
'sort
Call SortMeOut(myArrDate)
'list after sort from A-Z order
For i = LBound(myArrDate) To UBound(myArrDate)
mylen = Len(myArrDate(i))
temp = Right(myArrDate(i), mylen - 8)
List1.AddItem temp
Next i
End Sub