Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2005

Tip: Looking for answers? Try searching our database.

Directory.GetFiles File Order Issue

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Steve - 24 Aug 2005 03:37 GMT
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!
cody - 24 Aug 2005 08:57 GMT
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.

>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
[quoted text clipped - 8 lines]
> the XP file explorer view when sorted by the name column.
> Thanks in advance!
Steve - 24 Aug 2005 16:05 GMT
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.

> 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.
[quoted text clipped - 11 lines]
> > the XP file explorer view when sorted by the name column.
> > Thanks in advance!
Mattias Sjögren - 24 Aug 2005 16:43 GMT
>What I’m looking for is the class/api that performs the same sort by name as
>in the XP File Explorer.

StrCmpLogical

http://msdn.microsoft.com/library/en-us/shellcc/platform/shell/reference/shlwapi
/string/strcmplogicalw.asp


Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Steve - 24 Aug 2005 18:46 GMT
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

> >What I’m looking for is the class/api that performs the same sort by name as
> >in the XP File Explorer.
[quoted text clipped - 4 lines]
>
> Mattias
Jeremy Chapman - 31 Aug 2005 00:03 GMT
Instead of calling StrLogicalCompare, use your own function:

take the first file name, loop through each character in the file, if it's
numeric append it to one string, otherwise append it to a second string.
convert the first string to an integer.  Do the same for the second file
name.

then compare the 2 strings and 2 integers.

> Thanks Cody and Mattias:
> What I did was roll my own compare class that called the StrCmpLogicalW
[quoted text clipped - 29 lines]
>>
>> Mattias

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.