Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sorting files by date/time

0 views
Skip to first unread message

Peter

unread,
Nov 17, 2008, 6:08:19 AM11/17/08
to
Does anybody have a good VB6 sample code that sorts a list of files
retrieved using FindFirstFile/FindNextFile by date/time?
Regards
Peter

Dave O.

unread,
Nov 17, 2008, 6:37:36 AM11/17/08
to

"Peter" <pet...@myrealbox.com> wrote in message
news:gfrjb3$koe$1...@mail1.sbs.de...

There is no way (to my knowledge) to tell the
FindFirstFile/FindNextFile/FindClose to return files in any order other than
what you get.
So you need to read them in to an array or a ListView or whatever and then
sort them in the usual manner.

Dave O.


Peter

unread,
Nov 17, 2008, 7:40:32 AM11/17/08
to
Thanks for your fast answer!
What is the best way to sort the files by date/time:
Should I create a FILETIME array and use API function CompareFileTime() in
my sort routine?
Any sample code available?
Regards
Peter

"Dave O." <nob...@nowhere.com> schrieb im Newsbeitrag
news:OaYx5jKS...@TK2MSFTNGP05.phx.gbl...

Dave O.

unread,
Nov 17, 2008, 8:05:22 AM11/17/08
to

"Peter" <pet...@myrealbox.com> wrote in message
news:gfroo1$8qd$1...@mail1.sbs.de...

> Thanks for your fast answer!
> What is the best way to sort the files by date/time:
> Should I create a FILETIME array and use API function CompareFileTime() in
> my sort routine?

You could, but why bother, if you put the timestamp into a date variable you
can compare them directly in the sort routine.
If the data/times are stored as strings you'll need to convert them back to
dates for the sort, alternatively you could convert the date/time to doubles
and sort on those.
It's not all that easy to give exact examples because selecting which
routine would be optimum depends on many factors.

Dave O.


Dave O.

unread,
Nov 17, 2008, 8:14:23 AM11/17/08
to

"Dave O." <nob...@nowhere.com> wrote in message
news:%23lLX9UL...@TK2MSFTNGP03.phx.gbl...

>
> "Peter" <pet...@myrealbox.com> wrote in message
> news:gfroo1$8qd$1...@mail1.sbs.de...
>> Thanks for your fast answer!
>> What is the best way to sort the files by date/time:
>> Should I create a FILETIME array and use API function CompareFileTime()
>> in my sort routine?

I just remembered you MUST apply the FileTimeToLocalFileTime API to the
FileTime structure before you save the value anywhere, this is because the
file stamps are stored in UDT (GMT no summer adjustment) and will need to be
adjusted to your locale.

Regards
Dave O.


expvb

unread,
Nov 17, 2008, 8:24:32 AM11/17/08
to
"Peter" <pet...@myrealbox.com> wrote in message
news:gfrjb3$koe$1...@mail1.sbs.de...

Search for "FindFirstFile" at the following site:

http://vbnet.mvps.org

The site is for VB6 despite its name. As for comparing times, you can use
CompareFileTime, or convert it to a Date data type. For the later, see
FileTimeToVBDate() function here:

http://vbnet.mvps.org/code/fileapi/fsoapicompare.htm


Peter

unread,
Nov 17, 2008, 3:46:36 PM11/17/08
to
Thanks, but I think I don't need to use FileTimeToLocalFileTime() just to
sort the files by date/time, am I right?

Regards
Peter
"Dave O." <nob...@nowhere.com> schrieb im Newsbeitrag
news:%23bPO$ZLSJH...@TK2MSFTNGP06.phx.gbl...

Thorsten Albers

unread,
Nov 17, 2008, 5:23:32 PM11/17/08
to
Peter <-> schrieb im Beitrag <elTDFYPS...@TK2MSFTNGP02.phx.gbl>...

> Thanks, but I think I don't need to use FileTimeToLocalFileTime() just to

> sort the files by date/time, am I right?

You don't. In addition it is not necessary to call CompareFileTime() since
a FIILETIME is just an increasing 64 bit number. The call to
CompareFileTime() needs more time than a comparison done in you code.

If FT_A.dwHighDateTime <> FT_B.dwHighDateTime Then

If FT_A.dwHighDateTime >= 0 Then
If FT_B.dwHighDateTime >= 0 Then
If FT_A.dwHighDateTime > FT_B.dwHighDateTime Then
' A >= 0 / B >= 0 - A > B
Else
' A >= 0 / B >= 0 - A < B
End If
Else
' A >= 0 / B < 0: A < B
End If
ElseIf FT_B.dwHighDateTime >= 0 Then
' A < 0 / B >= 0: A > B
ElseIf FT_A.dwHighDateTime > FT_B.dwHighDateTime Then
' A < 0 / B < 0 - A > B
Else
' A < 0 / B < 0 - A < B
End If

ElseIf FT_A.dwLowDateTime >= 0 Then
If FT_B.dwLowDateTime >= 0 Then
If FT_A.dwLowDateTime > FT_B.dwLowDateTime Then
' A >= 0 / B >= 0 - A > B
Else
' A >= 0 / B >= 0 - A < B
End If
Else
' A >= 0 / B < 0: A < B
End If
ElseIf FT_B.dwLowDateTime >= 0 Then
' A < 0 / B >= 0: A > B
ElseIf FT_A.dwLowDateTime > FT_B.dwLowDateTime Then
' A < 0 / B < 0 - A > B
Else
' A < 0 / B < 0 - A < B
End If

--
----------------------------------------------------------------------
Thorsten Albers albers(a)uni-freiburg.de
----------------------------------------------------------------------

Larry Serflaten

unread,
Nov 17, 2008, 7:35:05 PM11/17/08
to

"Peter" <-> wrote in message news:elTDFYPS...@TK2MSFTNGP02.phx.gbl...

> Thanks, but I think I don't need to use FileTimeToLocalFileTime() just to
> sort the files by date/time, am I right?
> Regards

You might convert the values to the Decimal type which can handle
those large numbers. Then you just compare the two (Decimal)
values. Conversion might be like: (not tested)

Private Function ToDecimal(FileTime As FileTimeType) As Variant
Dim hi As Long, lo As Long
Dim hx As String
hx = Right$("0000000" & Hex(FileTime.Hi), 8)
hi = Val("&H" & Left$(hx, 4) & "&")
lo = Val("&H" & Right$(hx, 4) & "&")
ToDecimal = hi * CDec(65536) + lo
hx = Right$("0000000" & Hex(FileTime.Lo), 8)
hi = Val("&H" & Left$(hx, 4) & "&")
lo = Val("&H" & Right$(hx, 4) & "&")
ToDecimal = ToDecimal * CDec(4294967296#) + hi * CDec(65536) + lo
End Function

The conversion to Hex was used to properly treat the sign bit as used
in an unsigned value. Other (all numeric) methods of conversion may be
quicker....

LFS


Peter

unread,
Nov 21, 2008, 1:55:21 AM11/21/08
to
Just want to Thank you all for your answers.
I solved the problem by using a Function "FileTimetoserial" which creates a
Date-Variable out of the FileTime Structure.
Then I used these date values in my sort routine.
Regards
Peter

"Larry Serflaten" <serf...@usinternet.com> schrieb im Newsbeitrag
news:uQhAe3RS...@TK2MSFTNGP05.phx.gbl...

Thorsten Albers

unread,
Nov 21, 2008, 6:56:16 AM11/21/08
to
Peter <pet...@myrealbox.com> schrieb im Beitrag
<gg5m10$cvn$1...@mail1.sbs.de>...

> Just want to Thank you all for your answers.
> I solved the problem by using a Function "FileTimetoserial" which creates
a
> Date-Variable out of the FileTime Structure.
> Then I used these date values in my sort routine.

What a completely superfluous performance killer...

Peter

unread,
Nov 24, 2008, 10:27:42 AM11/24/08
to
You are right. But I found out that I need the filetime in a variable "as
date" later in my code so I thought I convert it to date immediately.
Regards
Peter

"Thorsten Albers" <albe...@MOVEuni-freiburg.de> schrieb im Newsbeitrag
news:01c94bd0$282ba700$af01a8c0@Router...

0 new messages