Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > Directory list sort order

Reply
Thread Tools

Directory list sort order

 
 
Jake
Guest
Posts: n/a
 
      01-29-2005
Im using the following code to display the contents of a directory:

<%
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFiles = objFso.GetFolder(Server.MapPath("."))
Set fileList = objFiles.Files
For Each i in fileList
response.write i.name & " - " & i.DateCreated & "<br>"
next
%>
Is there a way I can sort this by Date Created so the most recent files are
on top?

Thanks!


 
Reply With Quote
 
 
 
 
McKirahan
Guest
Posts: n/a
 
      01-29-2005
"Jake" <> wrote in message
news:#...
> Im using the following code to display the contents of a directory:
>
> <%
> Set objFso = CreateObject("Scripting.FileSystemObject")
> Set objFiles = objFso.GetFolder(Server.MapPath("."))
> Set fileList = objFiles.Files
> For Each i in fileList
> response.write i.name & " - " & i.DateCreated & "<br>"
> next
> %>
> Is there a way I can sort this by Date Created so the most recent files

are
> on top?
>
> Thanks!


Normally, I'd use ADO to sort the file names but ...

Files are returned in ascending DateCreated sequence;
thus, building an array and report it backwards will work.


<%
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFiles = objFso.GetFolder(Server.MapPath("."))
Set fileList = objFiles.Files
'*
Dim arrFiles()
Dim intFiles
intFiles = 0
For Each i In fileList
intFiles = intFiles + 1
ReDim Preserve arrFiles(intFiles)
arrFiles(intFiles) = i.name & " - " & i.DateCreated
Next
'*
For intFiles = UBound(arrFiles) To 1 Step -1
response.write arrFiles(intFiles) & "<br>"
Next
'*
Set objFso = Nothing
Set objFiles = Nothing
%>


 
Reply With Quote
 
 
 
 
Jake
Guest
Posts: n/a
 
      01-29-2005
Thanks for the reply -
Any way to sort this by Date Created Acending? Its now sorting by filename.



"McKirahan" <> wrote in message
news:SL2dnaqsVb6GsmbcRVn-...
> "Jake" <> wrote in message
> news:#...
>> Im using the following code to display the contents of a directory:
>>
>> <%
>> Set objFso = CreateObject("Scripting.FileSystemObject")
>> Set objFiles = objFso.GetFolder(Server.MapPath("."))
>> Set fileList = objFiles.Files
>> For Each i in fileList
>> response.write i.name & " - " & i.DateCreated & "<br>"
>> next
>> %>
>> Is there a way I can sort this by Date Created so the most recent files

> are
>> on top?
>>
>> Thanks!

>
> Normally, I'd use ADO to sort the file names but ...
>
> Files are returned in ascending DateCreated sequence;
> thus, building an array and report it backwards will work.
>
>
> <%
> Set objFso = CreateObject("Scripting.FileSystemObject")
> Set objFiles = objFso.GetFolder(Server.MapPath("."))
> Set fileList = objFiles.Files
> '*
> Dim arrFiles()
> Dim intFiles
> intFiles = 0
> For Each i In fileList
> intFiles = intFiles + 1
> ReDim Preserve arrFiles(intFiles)
> arrFiles(intFiles) = i.name & " - " & i.DateCreated
> Next
> '*
> For intFiles = UBound(arrFiles) To 1 Step -1
> response.write arrFiles(intFiles) & "<br>"
> Next
> '*
> Set objFso = Nothing
> Set objFiles = Nothing
> %>
>
>



 
Reply With Quote
 
McKirahan
Guest
Posts: n/a
 
      01-29-2005
"Jake" <> wrote in message
news:...
> Thanks for the reply -
> Any way to sort this by Date Created Acending? Its now sorting by

filename.
>
>
>
> "McKirahan" <> wrote in message
> news:SL2dnaqsVb6GsmbcRVn-...
> > "Jake" <> wrote in message
> > news:#...
> >> Im using the following code to display the contents of a directory:
> >>
> >> <%
> >> Set objFso = CreateObject("Scripting.FileSystemObject")
> >> Set objFiles = objFso.GetFolder(Server.MapPath("."))
> >> Set fileList = objFiles.Files
> >> For Each i in fileList
> >> response.write i.name & " - " & i.DateCreated & "<br>"
> >> next
> >> %>
> >> Is there a way I can sort this by Date Created so the most recent files

> > are
> >> on top?
> >>
> >> Thanks!

> >
> > Normally, I'd use ADO to sort the file names but ...
> >
> > Files are returned in ascending DateCreated sequence;
> > thus, building an array and report it backwards will work.


[snip]

I guess it's only my PC that displays files by DateCreated.

Here's a subroutine that may do what you want.

<%@ Language="VBScript" %>
<% Option Explicit
Call FileSort(".")

Sub FileSort(folder)
'****
'*
'* This subroutine lists files in a folder by DateCreated.
'*
'****
'*
'* Declare Constants
'*
Const cRS1 = "DateCreated"
Const cRS2 = "FileName"
'*
Const adChar = 129
Const adDate = 7
Const adLockBatchOptimistic = 4
Const adOpenStatic = 3
Const adUseClient = 3
'*
'* Declare Objects
'*
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objGFO
Set objGFO = objFso.GetFolder(Server.MapPath(folder))
Dim objFIL
Set objFIL = objGFO.Files
Dim objRST
Set objRST = CreateObject("ADODB.RecordSet")
objRST.CursorLocation = adUseClient
objRST.LockType = adLockBatchOptimistic
objRST.CursorType = adOpenStatic
objRST.ActiveConnection = Nothing
objRST.Fields.Append cRS1, adDate
objRST.Fields.Append cRS2, adChar, 255
objRST.Open
'*
'* Files
'*
Dim intFIL
For Each intFIL In objFIL
objRST.AddNew
objRST.Fields(cRS1) = intFIL.DateCreated
objRST.Fields(cRS2) = intFIL.name
objRST.Update
Next
'*
'* Sort RecordSet
'*
objRST.Sort = cRS1 & " DESC"
objRST.MoveFirst
'*
'* Read RecordSet
'*
Do While Not objRST.EOF
Response.Write("<br>" & Trim(objRST.Fields(cRS2)) & " = " &
objRST.Fields(cRS1))
objRST.MoveNext
Loop
'*
'* Destroy Objects
'*
objRST.Close
Set objRST = Nothing
Set objFIL = Nothing
Set objGFO = Nothing
Set objFSO = Nothing
End Sub
%>


Of course you could always Shell out to the "dir" command.

<%@ Language="VBScript" %>
<% Option Explicit
Const cTXT = "~dir_o-d.txt"
Const cWSS = "%comspec% /c dir /o-d > "
Dim strWSS
strWSS = Server.MapPath(cTXT)
Dim objWSS
Set objWSS = CreateObject("WScript.Shell")
objWSS.Run cWSS & strWSS, 0, True
Set objWSS = Nothing
%>

Then use FSO to read this file.


 
Reply With Quote
 
Jake
Guest
Posts: n/a
 
      01-29-2005
Thanks!


"McKirahan" <> wrote in message
news:FKKdnU3ZNqxE12bcRVn-...
> "Jake" <> wrote in message
> news:...
>> Thanks for the reply -
>> Any way to sort this by Date Created Acending? Its now sorting by

> filename.
>>
>>
>>
>> "McKirahan" <> wrote in message
>> news:SL2dnaqsVb6GsmbcRVn-...
>> > "Jake" <> wrote in message
>> > news:#...
>> >> Im using the following code to display the contents of a directory:
>> >>
>> >> <%
>> >> Set objFso = CreateObject("Scripting.FileSystemObject")
>> >> Set objFiles = objFso.GetFolder(Server.MapPath("."))
>> >> Set fileList = objFiles.Files
>> >> For Each i in fileList
>> >> response.write i.name & " - " & i.DateCreated & "<br>"
>> >> next
>> >> %>
>> >> Is there a way I can sort this by Date Created so the most recent
>> >> files
>> > are
>> >> on top?
>> >>
>> >> Thanks!
>> >
>> > Normally, I'd use ADO to sort the file names but ...
>> >
>> > Files are returned in ascending DateCreated sequence;
>> > thus, building an array and report it backwards will work.

>
> [snip]
>
> I guess it's only my PC that displays files by DateCreated.
>
> Here's a subroutine that may do what you want.
>
> <%@ Language="VBScript" %>
> <% Option Explicit
> Call FileSort(".")
>
> Sub FileSort(folder)
> '****
> '*
> '* This subroutine lists files in a folder by DateCreated.
> '*
> '****
> '*
> '* Declare Constants
> '*
> Const cRS1 = "DateCreated"
> Const cRS2 = "FileName"
> '*
> Const adChar = 129
> Const adDate = 7
> Const adLockBatchOptimistic = 4
> Const adOpenStatic = 3
> Const adUseClient = 3
> '*
> '* Declare Objects
> '*
> Dim objFSO
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Dim objGFO
> Set objGFO = objFso.GetFolder(Server.MapPath(folder))
> Dim objFIL
> Set objFIL = objGFO.Files
> Dim objRST
> Set objRST = CreateObject("ADODB.RecordSet")
> objRST.CursorLocation = adUseClient
> objRST.LockType = adLockBatchOptimistic
> objRST.CursorType = adOpenStatic
> objRST.ActiveConnection = Nothing
> objRST.Fields.Append cRS1, adDate
> objRST.Fields.Append cRS2, adChar, 255
> objRST.Open
> '*
> '* Files
> '*
> Dim intFIL
> For Each intFIL In objFIL
> objRST.AddNew
> objRST.Fields(cRS1) = intFIL.DateCreated
> objRST.Fields(cRS2) = intFIL.name
> objRST.Update
> Next
> '*
> '* Sort RecordSet
> '*
> objRST.Sort = cRS1 & " DESC"
> objRST.MoveFirst
> '*
> '* Read RecordSet
> '*
> Do While Not objRST.EOF
> Response.Write("<br>" & Trim(objRST.Fields(cRS2)) & " = " &
> objRST.Fields(cRS1))
> objRST.MoveNext
> Loop
> '*
> '* Destroy Objects
> '*
> objRST.Close
> Set objRST = Nothing
> Set objFIL = Nothing
> Set objGFO = Nothing
> Set objFSO = Nothing
> End Sub
> %>
>
>
> Of course you could always Shell out to the "dir" command.
>
> <%@ Language="VBScript" %>
> <% Option Explicit
> Const cTXT = "~dir_o-d.txt"
> Const cWSS = "%comspec% /c dir /o-d > "
> Dim strWSS
> strWSS = Server.MapPath(cTXT)
> Dim objWSS
> Set objWSS = CreateObject("WScript.Shell")
> objWSS.Run cWSS & strWSS, 0, True
> Set objWSS = Nothing
> %>
>
> Then use FSO to read this file.
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
[B,IX] = sort(A,...) - Order for sort()-function =?iso-8859-1?Q?=22Orlando_D=F6hring=22?= Python 0 05-29-2007 03:31 PM
xsl:sort lang="es" modern vs. tradidional Spanish sort order nobody XML 0 06-01-2004 06:25 AM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM
How do you sort a name list and list in random order? Frank & Janny Plaza Python 4 09-23-2003 01:51 AM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM



Advertisments
 



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 47 48 49 50 51 52 53 54 55 56 57