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

Sort files in a directory

32 views
Skip to first unread message

David C

unread,
Jul 2, 2008, 1:03:54 PM7/2/08
to
I would like to sort the list of files (documents) that I am retrieving into
a DataGrid control when they are first displayed. Currently they just list
in the order that they appear in the directory and I would like to sort them
on LastWriteTime property. Below is what I am using to populate the
DataGrid. Thanks.

David


Dim dirInfo As New DirectoryInfo(strPathPhy)
articleList.DataSource = dirInfo.GetFiles()
articleList.DataBind()

sloan

unread,
Jul 2, 2008, 1:32:17 PM7/2/08
to

http://morewally.com/cs/blogs/wallym/archive/2007/08/10/visual-studio-2008-friday-august-10-2007.aspx

Looks like you need to specify the version of the framework if url doesn't
meet your need.

"David C" <dlc...@lifetimeinc.com> wrote in message
news:Og3YzWG3...@TK2MSFTNGP02.phx.gbl...

David C

unread,
Jul 2, 2008, 1:46:04 PM7/2/08
to
I am running on VS 2005 and .Net 2.0

David
"sloan" <sl...@ipass.net> wrote in message
news:OBmFbkG3...@TK2MSFTNGP03.phx.gbl...

siccolo

unread,
Jul 2, 2008, 4:06:25 PM7/2/08
to

something like this, perhaps: (see at
http://www.siccolo.com/Articles/CodeProject/SelectFileDlg_SmartPhone/Smartphone_WM6_FileDialog.html)
...
Dim FileInfoComparer As FileInfoComparer = New FileInfoComparer()
Dim FileList() As FileInfo = ParentFolder.GetFiles()
Array.Sort(FileList, FileInfoComparer)
...
where
Public Class FileInfoComparer Implements
System.Collections.IComparer
Public Function Compare(ByVal objFileInfo1 As Object, ByVal
objFileInfo2 As Object) As Integer _
Implements IComparer.Compare
Dim FileInfo1 As System.IO.FileInfo = CType(objFileInfo1,
System.IO.FileInfo)
Dim FileInfo2 As System.IO.FileInfo = CType(objFileInfo2,
System.IO.FileInfo)

Return String.Compare(FileInfo1.Name, FileInfo2.Name, True)
End Function
End Class

more at http://www.siccolo.com/articles.asp


SAL

unread,
Jul 2, 2008, 4:41:18 PM7/2/08
to
I'm assuming you mean a GridView control since I don't see a control on the
Data tab called a DataGrid. If that's the case you can just use the
GridView's sort method

GridView1.Sort("FieldName", SortDirection)

HTH

S

"David C" <dlc...@lifetimeinc.com> wrote in message
news:Og3YzWG3...@TK2MSFTNGP02.phx.gbl...

Mark Rae [MVP]

unread,
Jul 2, 2008, 5:00:32 PM7/2/08
to
"SAL" <S...@nospam.nospam> wrote in message
news:uC0y7PI3...@TK2MSFTNGP04.phx.gbl...

> I'm assuming you mean a GridView control since I don't see a control on
> the Data tab called a DataGrid.

http://aspnet.4guysfromrolla.com/articles/040502-1.aspx


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

SAL

unread,
Jul 2, 2008, 5:50:04 PM7/2/08
to
Oh, that version. Sorry... :)

S

"Mark Rae [MVP]" <ma...@markNOSPAMrae.net> wrote in message
news:uB12qaI3...@TK2MSFTNGP03.phx.gbl...

Milosz Skalecki [MCAD]

unread,
Jul 2, 2008, 6:37:06 PM7/2/08
to
Hi there,

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

If Not IsPostBack Then

Dim files As FileInfo() = New DirectoryInfo("c:\temp").GetFiles()

Array.Sort(Of FileInfo)(files, AddressOf FileInfoComparison)

articleList.DataSource = files
articleList.DataBind()

End If

End Sub

Private Shared Function FileInfoComparison(ByVal fi1 As FileInfo, ByVal
fi2 As FileInfo) As Integer
Return Date.Compare(fi1.LastWriteTime, fi2.LastWriteTime)
End Function

--
Milosz

jc

unread,
Jul 15, 2008, 6:48:09 PM7/15/08
to
Thanks.. but this does not seem to work for me. Produces the images in
a random order.

thanks.

Imports System.Collections
Imports System.IO

Partial Class _Default
Inherits System.Web.UI.Page


Sub Page_Load()

Dim pictures As New SortedList
Dim filename As String = ""
Dim i As Integer

Dim files As FileInfo() = New DirectoryInfo("c:\ppp\pictures
\").GetFiles()

Array.Sort(Of FileInfo)(files, AddressOf FileInfoComparison)


For i = 0 To files.Length - 1

filename = files(i).Name.ToLower()

If filename.EndsWith(".jpg") Then
pictures("pictures/" & filename) = "thumb.aspx?
src=pictures/" & filename
End If

Repeater1.DataSource = pictures
Page.DataBind()
Next


End Sub


Private Shared Function FileInfoComparison(ByVal fi1 As FileInfo,
ByVal fi2 As FileInfo) As Integer
Return Date.Compare(fi1.LastWriteTime, fi2.LastWriteTime)
End Function

End Class


Lloyd Sheen

unread,
Jul 15, 2008, 7:41:31 PM7/15/08
to

"jc" <jo...@webdos.com> wrote in message
news:e78fc297-b4ba-4202...@l42g2000hsc.googlegroups.com...

Seems to me that you are sorting the list ok in files but then you are
creating a new SortedList which has nothing to do with the sort you did on
LastWriteTime. The list is most likely sorted on the keys.

LS

jc

unread,
Jul 16, 2008, 10:54:00 AM7/16/08
to

> Seems to me that you are sorting the list ok in files but then you are
> creating a new SortedList which has nothing to do with the sort you did on
> LastWriteTime.  The list is most likely sorted on the keys.

How can I loop through files in the new sorted order if not by the
index?

Any suggestion on fixing the code to do what I want?

Thanks.

Lloyd Sheen

unread,
Jul 16, 2008, 11:11:02 AM7/16/08
to

"jc" <jo...@webdos.com> wrote in message
news:e7d8273c-610b-4099...@k37g2000hsf.googlegroups.com...

Thanks.

You have the sorted list from the array sort. Use that array.

LS

jc

unread,
Jul 16, 2008, 12:38:28 PM7/16/08
to

> You have the sorted list from the array sort.  Use that array.
>
> LS


Sorry.. I'm missing something here..

This line:

Array.Sort(Of FileInfo)(files, AddressOf FileInfoComparison)

is sorting the array "files" right?

How can I loop through "files" in the order it was sorted in using
vb.net?

thanks.. if possible can you paste the code?

Lloyd Sheen

unread,
Jul 16, 2008, 1:03:24 PM7/16/08
to

"jc" <jo...@webdos.com> wrote in message
news:f0080c8a-674d-4306...@59g2000hsb.googlegroups.com...

This line:

Array.Sort(Of FileInfo)(files, AddressOf FileInfoComparison)


for each fi as FileInfo in files

do something

next

jc

unread,
Jul 16, 2008, 6:25:05 PM7/16/08
to
Thank you.. but this code is still not returing the images by name or
datetime order?


Imports System.Collections
Imports System.IO

Partial Class _Default
Inherits System.Web.UI.Page


Sub Page_Load()

Dim pictures As New SortedList
Dim filename As String = ""

Dim files As FileInfo() = New DirectoryInfo("c:\jcp\pic
\pictures\").GetFiles()

Array.Sort(Of FileInfo)(files, AddressOf FileInfoComparison)


For Each fi As FileInfo In files

filename = fi.Name.ToLower()


If filename.EndsWith(".jpg") Then
pictures("pictures/" & filename) = "thumb.aspx?
src=pictures/" & filename
End If

Next

Repeater1.DataSource = pictures
Page.DataBind()
End Sub


Private Shared Function FileInfoComparison(ByVal fi1 As FileInfo,
ByVal fi2 As FileInfo) As Integer
'Return Date.Compare(fi1.LastWriteTime, fi2.LastWriteTime)

Return String.Compare(fi1.Name, fi2.Name)
End Function


End Class

I noticed if I use Hashtable instead of sortedlist I get yet another
order - neither correct ... I need to store the file names plus other
information for processing.. Sorry and Thanks any additional help.

Lloyd Sheen

unread,
Jul 17, 2008, 10:53:02 AM7/17/08
to

"jc" <jo...@webdos.com> wrote in message
news:c5515e7c-4913-47f8...@j22g2000hsf.googlegroups.com...

The problem is the other collection (pictures). You sort and then when you
populate pictures your sort is destroyed. Make your datasource files and
you are ok. You will have to massage the info to get pictures/filename but
that is not much work.

LS

0 new messages