Directory.GetFiles File Order Issue

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to programmatically iterate over all files in a folder in order of
file name. I’m using VB.Net and Directory.GetFiles. The problem is that the
numbers are being interpreted as characters versus numbers as the default in
XP. Hence my iterations are Image1.jpg, Image10.jpg, Image11.jpg,
Image2.jpg, Image3.jpg…

I’m looking for a way to have the files in correct numeric order similar to
the XP file explorer view when sorted by the name column.
Thanks in advance!
 
You have to sort the files by yourself, GetFiles cannot do it.
Parse the filename, get the number out of it and sort by the number.
 
Thanks that would work. Any idea how to handle sorting of mixed alphabetic
and alphanumeric file names so that the digits located anywhere in the
filename are treated as numbers vs. characters?

What I’m looking for is the class/api that performs the same sort by name as
in the XP File Explorer.
 
Thanks Cody and Mattias:
What I did was roll my own compare class that called the StrCmpLogicalW
function. Unfortunately this limits my users to XP only since it's not
available on 2000.
I need to check for string nulls but below is a code fragment to sort file
names as in the XP File Explorer:


Declare Unicode Function StrLogicalComparer Lib "shlwapi.dll" _
Alias "StrCmpLogicalW" (ByVal str1 As String, ByVal str2 As String) As
Integer

Public Class StrLogicalComparerClass
Implements IComparer

Function Compare(ByVal x As [Object], ByVal y As [Object]) As
Integer _
Implements IComparer.Compare
Dim text1, text2 As String
text1 = CStr(x)
text2 = CStr(y)
Return StrLogicalComparer(text1, text2)
End Function 'IComparer.Compare
End Class 'StrLogicalComparerClass
 
Back
Top