Go Back   Velocity Reviews > Newsgroups > ASP Net
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

ASP Net - Sort files in a directory

 
Thread Tools Search this Thread
Old 07-02-2008, 06:03 PM   #1
Default Sort files in a directory


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





David C
  Reply With Quote
Old 07-02-2008, 06:32 PM   #2
sloan
 
Posts: n/a
Default Re: Sort files in a directory

http://morewally.com/cs/blogs/wallym...t-10-2007.aspx

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





"David C" <> wrote in message
news:...
>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
  Reply With Quote
Old 07-02-2008, 06:46 PM   #3
David C
 
Posts: n/a
Default Re: Sort files in a directory
I am running on VS 2005 and .Net 2.0

David
"sloan" <> wrote in message
news:...
>
> http://morewally.com/cs/blogs/wallym...t-10-2007.aspx
>
> Looks like you need to specify the version of the framework if url doesn't
> meet your need.
>
>
>
>
>
> "David C" <> wrote in message
> news:...
>>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()
>>
>>
>>

>
>





David C
  Reply With Quote
Old 07-02-2008, 09:06 PM   #4
siccolo
 
Posts: n/a
Default Re: Sort files in a directory
On Jul 2, 1:03*pm, "David C" <dlch...@lifetimeinc.com> wrote:
> 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/Code...ileDialog.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




siccolo
  Reply With Quote
Old 07-02-2008, 09:41 PM   #5
SAL
 
Posts: n/a
Default Re: Sort files in a directory
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" <> wrote in message
news:...
>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()
>
>
>





SAL
  Reply With Quote
Old 07-02-2008, 10:50 PM   #6
SAL
 
Posts: n/a
Default Re: Sort files in a directory
Oh, that version. Sorry...

S

"Mark Rae [MVP]" <> wrote in message
news:...
> "SAL" <> wrote in message
> news:...
>
>> 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
  Reply With Quote
Old 07-02-2008, 11:37 PM   #7
Milosz Skalecki [MCAD]
 
Posts: n/a
Default RE: Sort files in a directory
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


"David C" wrote:

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



Milosz Skalecki [MCAD]
  Reply With Quote
Old 07-15-2008, 11:48 PM   #8
jc
 
Posts: n/a
Default Re: Sort files in a directory
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
  Reply With Quote
Old 07-16-2008, 12:41 AM   #9
Lloyd Sheen
 
Posts: n/a
Default Re: Sort files in a directory

"jc" <> wrote in message
news:e78fc297-b4ba-4202-827f-...
> 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



Lloyd Sheen
  Reply With Quote
Old 07-16-2008, 03:54 PM   #10
jc
 
Posts: n/a
Default Re: Sort files in a directory

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


jc
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to get total bytes of all files from directory AlbertM. Software 1 08-06-2008 01:27 AM
Convert Video files to PSP ivan DVD Video 4 06-17-2008 11:16 AM
Convert Video files to MP4 for iPod ivan DVD Video 0 04-26-2006 08:38 AM
Very slow recognising DVD disc Terry Pinnell DVD Video 1 03-28-2006 06:53 PM
Now I introduce some popular software of multimedia eightsome@gmail.com DVD Video 0 03-28-2006 02:29 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46