Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dir() but ordering files 1

Status
Not open for further replies.

pwebonline

Programmer
Oct 9, 2002
6
AR
Hello everyone, I am using the function 'dir' like this:

File = Dir("C:\*.TXT")

The problem is that I need to be able to tell it somehow to return the oldest file. I could not find how to send it a parameter with an order or whatever. Any idea?
 

Make a list (list1) or a collection of the files returned by your function. Then run through the list using the FileDateTime function to return the datetime of the files.

Like so...

[tt]
Private Sub Command1_Click()

Dim C As New Collection, T As String, I As Integer, D As Date

T = Dir("c:\*.txt")
C.Add "c:\" & T
Do While T <> &quot;&quot;
T = Dir
If T <> &quot;&quot; Then C.Add &quot;c:\&quot; & T
Loop

D = FileDateTime(C.Item(1))
T = C.Item(1)
For I = 2 To C.Count
If C.Item(I) = &quot;&quot; Then Exit For
If FileDateTime(C.Item(I)) < D Then
T = C.Item(I)
D = FileDateTime(C.Item(I))
End If
Next I

MsgBox &quot;The oldest file is &quot; & T, vbOKOnly + vbInformation, &quot;Done&quot;


End Sub


[/tt]

Good Luck
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top