I need to find the file that has the newest date in a given directory. I’m less than a beginner in VB6 and that’s what I need to use for some old code. After killing Google all day below is what I have so far. But it doesnt find the right file. Can someone help please?
Thanks
Dim sFile1 As String
Dim sFile2 As String
Dim dFile1Date As Date
Dim dFile2Date As Date
sFile1 = Dir("c:\test\*.*", vbNormal)
Do
sFile2 = Dir
If sFile1 <> "" Then dFile1Date = FileDateTime("c:\test\" & sFile1)
If sFile2 <> "" Then dFile2Date = FileDateTime("c:\test\" & sFile2)
If dFile1Date > dFile2Date Then sFile2 = sFile1
Loop Until sFile2 = ""
FormVersionDate = sFile1
Call MsgBox(FormVersionDate, vbExclamation, App.Title)
If you step through your code, you’ll see that
sFile1never gets reassigned no matter what the results of your logic, so calling it’s value at the end makes all of your do loop irrelevant. Also it looks likeFormVersionDateis probably supposed to be a date, but you’re assign a string to it instead.I re-wrote your code with nested looping to avoid logic if it was irrelevant with excessive commenting: