Sort files in a directory

  • Thread starter Thread starter David C
  • Start date Start date
D

David C

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()
 
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()

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
 
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
 
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
 
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
 
jc said:
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

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
 
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.
 
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.

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

LS
 
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?
 
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?


for each fi as FileInfo in files

do something

next
 
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.
 
jc said:
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.

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
 
Back
Top