469,271 Members | 1,484 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Directory list sort order

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!
Jul 22 '05 #1
4 19721
"Jake" <sp******@alltel.net> wrote in message
news:#D**************@TK2MSFTNGP12.phx.gbl...
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
%>
Jul 22 '05 #2
Thanks for the reply -
Any way to sort this by Date Created Acending? Its now sorting by filename.

"McKirahan" <Ne**@McKirahan.com> wrote in message
news:SL********************@comcast.com...
"Jake" <sp******@alltel.net> wrote in message
news:#D**************@TK2MSFTNGP12.phx.gbl...
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
%>

Jul 22 '05 #3
"Jake" <sp******@alltel.net> wrote in message
news:OA**************@tk2msftngp13.phx.gbl...
Thanks for the reply -
Any way to sort this by Date Created Acending? Its now sorting by filename.
"McKirahan" <Ne**@McKirahan.com> wrote in message
news:SL********************@comcast.com...
"Jake" <sp******@alltel.net> wrote in message
news:#D**************@TK2MSFTNGP12.phx.gbl...
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.
Jul 22 '05 #4
Thanks!
"McKirahan" <Ne**@McKirahan.com> wrote in message
news:FK********************@comcast.com...
"Jake" <sp******@alltel.net> wrote in message
news:OA**************@tk2msftngp13.phx.gbl...
Thanks for the reply -
Any way to sort this by Date Created Acending? Its now sorting by

filename.

"McKirahan" <Ne**@McKirahan.com> wrote in message
news:SL********************@comcast.com...
> "Jake" <sp******@alltel.net> wrote in message
> news:#D**************@TK2MSFTNGP12.phx.gbl...
>> 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.

Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by One's Too Many | last post: by
7 posts views Thread by Steve Crawford | last post: by
reply views Thread by joseph speigle | last post: by
2 posts views Thread by adrian.chandler | last post: by
5 posts views Thread by Ethan Strauss | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.