Help please: Traversing folderitems in a folder

Hello,

for i = myFolder.Count downto 1
myFile = myFolder.TrueItem(i)
will go through the myFile in myFolder alphabetically.

myFolder has a variety of files, several of which have, as part of their names, an sqldate, (something like 1954-05-17), as the fourth nthField(" "),
e.g… if myFile.name = “XY John Doe 1954-05-17”, NthField(myFile, " ", 4) would be 1954-05-17

Is there a way to go through the folder items via the sqldate in descending order (earliest date first)?

Thanks.

Lennox

go 1 to count - its slow to go backwards
you’d be better off to get a list of all folder items and put their creation or modification dates in another array
then use sort with then go though the array of folder items

di[code]m items() as folderitem
dim dates() as double // we’re gong to use total seconds from the creation date
for i = 1 to myFolder.Count
items.append myFolder.TrueItem(i)
dates.append myFolder.TrueItem(i).creationdate.totalseconds
next

dates.sortwith items
for i = 0 to ubound(items)
// do whatever with this item
next[/code]

Thanks Norman,

I’ll try it out and see.

Lennox

Changing

for i = 1 to myFolder.Count

to

Dim FolderCount As Integer = myFolder.Count For i = 1 to FolderCount

will also improve performance. FolderItem.Count is expensive, so don’t do it for every file in the folder.

Thanks Norman and Wayne,

The code snippets were very helpful, I appreciate it.

Works as expected, did not know about “sortwith” .

for i = 0 to ubound(items) will arrange from earliest date to latest date

I need latest date to earliest date

Just for my information… will this be expensive on memory?
for i = ubound(items) down to 0

The numbers are small, so if it is expensive, it will not hurt.

Thanks again.

Lennox

The above is just fine. It’s the folderitem that is expensive unless you’re stepping forward through the directory.

If you go peek at my code again you’ll see I grab the folderitem and whatever attribute it is I want to sort them by
Then use “sortwith” to order both arrays by that and go through the folder items

Norman how’d you manage to get my name with Lennox’s words?

no clue but its easy to fix :stuck_out_tongue:

Thanks.
Lennox