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

Ordering FSO output ?

1 view
Skip to first unread message

@nothere.com James

unread,
May 22, 2003, 5:12:11 PM5/22/03
to
Hi I'm getting a list of files in a folder using the following script:
script similar to :

set bjm= CreateObject("Scripting.FileSystemObject")
drive = "F:\Test\Backup"

set f = bjm.GetFolder(drive)
set fc = f.files

for each f1 in fc
Response.Write "<tr><td><a href='/" & f1.name & "'>"
Response.Write f1.name & "</a></td></tr><p>"
Next


What I would like to know is ::

Is it possible to do the above but specify the folder as '..\Backup' so the
script looks in Backup off the root of the web ??

Can the out put be in date order ??

How would I do this ??

Thanks

Randy

unread,
May 22, 2003, 5:17:05 PM5/22/03
to
> Can the out put be in date order ??

To put the files in any kind of order, you'll probably have to build a
recordset and populate it with the filename and date. Then you can sort the
recordset anyway you want.

Aaron Bertrand - MVP

unread,
May 22, 2003, 5:21:45 PM5/22/03
to
http://www.aspfaq.com/2025


"James" <James @ nothere.com> wrote in message
news:f1fqcvkc6fd1m1n2l...@4ax.com...

Egbert Nierop (MVP for IIS)

unread,
May 23, 2003, 1:51:09 AM5/23/03
to
"Aaron Bertrand - MVP" <aa...@TRASHaspfaq.com> wrote in message
news:ub$QRgKID...@TK2MSFTNGP10.phx.gbl...
> http://www.aspfaq.com/2025

Aaron,

The article that uses a database to sort, isn't that a little overdone?
ADODB.Recordset supports the .Sort method and needs no slow database :)

--

James

unread,
May 23, 2003, 3:53:34 AM5/23/03
to
So How would I sort using ADODB.Recordset ?

James

unread,
May 23, 2003, 3:54:53 AM5/23/03
to
On Thu, 22 May 2003 22:12:11 +0100, James <James @ nothere.com> wrote:

>Hi I'm getting a list of files in a folder using the following script:
>script similar to :
>
>set bjm= CreateObject("Scripting.FileSystemObject")
>drive = "F:\Test\Backup"
>
>set f = bjm.GetFolder(drive)
>set fc = f.files
>
>for each f1 in fc
> Response.Write "<tr><td><a href='/" & f1.name & "'>"
> Response.Write f1.name & "</a></td></tr><p>"
>Next
>
>
>What I would like to know is ::
>
>Is it possible to do the above but specify the folder as '..\Backup' so the
>script looks in Backup off the root of the web ??

How can I do the above, with out specifing the exact folder ie:
..\Backup instead off F:\Test\Backup

Bob Barrows

unread,
May 23, 2003, 7:07:41 AM5/23/03
to
Egbert Nierop (MVP for IIS) wrote:
> "Aaron Bertrand - MVP" <aa...@TRASHaspfaq.com> wrote in message
> news:ub$QRgKID...@TK2MSFTNGP10.phx.gbl...
>> http://www.aspfaq.com/2025
>
> Aaron,
>
> The article that uses a database to sort, isn't that a little
> overdone? ADODB.Recordset supports the .Sort method and needs no slow
> database :)
>
>
While an ad hoc recordset would probably be more efficient than sending the
data to a database table (network traffic, extra disk i/o, etc.), I wonder
why one would invoke a clunky recordset just to sort a few data elements.
Why not just put the data into an array and use a bubble sort (there can't
be that many filenames .... famous last words <grin>). I would think that
has to be more efficient that creating a COM object ...

Bob Barrows


Bob Barrows

unread,
May 23, 2003, 7:36:23 AM5/23/03
to
James wrote:
> Hi I'm getting a list of files in a folder using the following script:
> script similar to :
>
> set bjm= CreateObject("Scripting.FileSystemObject")
> drive = "F:\Test\Backup"
>
> set f = bjm.GetFolder(drive)
> set fc = f.files
>
> for each f1 in fc
> Response.Write "<tr><td><a href='/" & f1.name & "'>"
> Response.Write f1.name & "</a></td></tr><p>"
> Next
>
>
> What I would like to know is ::
>
> Is it possible to do the above but specify the folder as '..\Backup'
> so the script looks in Backup off the root of the web ??

You are using the wrong slash if you want to specify it by URL. The answer
is to use MapPath:

drive = Server.MapPath("../Backup")

>
> Can the out put be in date order ??
>

yes

> How would I do this ??

Write the data into a 2-dimensional array and use a bubble sort (this is
untested air code):

dim arFiles(),i,j,f,bjm,fc,fl,drive
dim tmpName, tmpDate


set bjm= CreateObject("Scripting.FileSystemObject")

drive = Server.MapPath("../Backup")


set f = bjm.GetFolder(drive)
set fc = f.files

redim arFiles(1,fc.count - 1)
for i = 0 to ubound(arFiles,2)
arFiles(0,i) = fl.name
'assuming you want the last-modified date
arFiles(1,i) = fl.DateLastModified
next
'now sort by date:
for i = 0 to ubound(arFiles,2) - 1
for j = i+1 to ubound(arFiles,2)
if cdate(arfiles(1,i)) > cdate(arfiles(1,j)) then
'swap them
tmpName = arFiles(0,j)
tmpDate = arFiles(1,j)
arFiles(0,j) = arfiles(0,i)
arFiles(1,j) = arfiles(1,i)
arfiles(0,i) = tmpName
arfiles(1,i) = tmpDate
end if
next
next
'now write the results
for i = 0 to ubound(arFiles,2)
Response.Write "<tr><td><a href='/" & arFiles(0,i) & "'>"
Response.Write arfiles(0,i) & "</a></td></tr><p>"
next


HTH,
Bob Barrows


Egbert Nierop (MVP for IIS)

unread,
May 23, 2003, 8:28:15 AM5/23/03
to
Quite simple...
Something like this (note, not complete, you should add the filereading
stuff, but you have that already)...
Set Rs = CreateObject("ADODB.Recordset")
Rs.CursorLocation = adUseClient
With Rs.Fields
.Append "filename", AD_BSTR, 128
.Append "size", adInteger
.Append "creationdate", adDate
End With

Rs.Open

Do Until [all files read]
Rs.AddNew
... add the stuff in a loop here
With Rs.Fields
.Item("filename").Value = fs.Name
.Item("size").Value = fs.FileSize
.Item("creationDate").Value = fs.CreationDate
End With
Loop

' HOWTO sort
Rs.Sort = "filename ASC"
or
Rs.Sort = "filename DESC"
or
Rs.Sort = "creationDate ASC"
etc that's it...


--
compatible web farm Session replacement for Asp and Asp.Net
http://www.nieropwebconsult.nl/asp_session_manager.htm

<James @ nothere.com (James)> wrote in message
news:3ecdd31...@news.btclick.com...

Egbert Nierop (MVP for IIS)

unread,
May 23, 2003, 8:23:49 AM5/23/03
to
"Bob Barrows" <reb_...@yahoo.com> wrote in message
news:OtlLFuRI...@TK2MSFTNGP11.phx.gbl...

> Egbert Nierop (MVP for IIS) wrote:
> > "Aaron Bertrand - MVP" <aa...@TRASHaspfaq.com> wrote in message
> be that many filenames .... famous last words <grin>). I would think that
> has to be more efficient that creating a COM object ...


Bubble sort in Vbscript?? Ha! Just go for a CPU upgrade :)

--

> Bob Barrows
>
>

Bob Barrows

unread,
May 23, 2003, 8:56:16 AM5/23/03
to
Egbert Nierop (MVP for IIS) wrote:
> "Bob Barrows" <reb_...@yahoo.com> wrote in message
> news:OtlLFuRI...@TK2MSFTNGP11.phx.gbl...
>> Egbert Nierop (MVP for IIS) wrote:
>>> "Aaron Bertrand - MVP" <aa...@TRASHaspfaq.com> wrote in message
>> be that many filenames .... famous last words <grin>). I would think
>> that has to be more efficient that creating a COM object ...
>
>
> Bubble sort in Vbscript?? Ha! Just go for a CPU upgrade :)
>
Really? I've had great success using the bubble sort. As long as the number
of items to be sorted is small (I've never used it with more than ~100), I
get excellent response, even on my old P166 machine ...

Someday I'll run a comparison ... :-)

Bob


Aaron Bertrand - MVP

unread,
May 23, 2003, 9:20:02 AM5/23/03
to
> The article that uses a database to sort, isn't that a little overdone?
> ADODB.Recordset supports the .Sort method and needs no slow database :)

Um, I take it you didn't read the whole article, Egbert.


Bob Barrows

unread,
May 23, 2003, 10:37:03 AM5/23/03
to
OK, I couldn't resist. "Someday" is today :-)

Using the code shown below, I got these results sorting a folder that
contained 173 files:
Bubble sort:
173 files - 7 seconds for 10 re-sorts.
Recordset sort:
173 files - 6 seconds for 10 re-sorts.

These results are very repeatable, so, as far as the performance time is
concerned, the two methods are about equivalent when run against a data set
this large, with the recordset just slightly beating the bubble sort to the
finish line. When I ran this code against a folder containing 63 files, both
methods reported 2 seconds for 10 re-sorts. When I increased the number of
re-sorts to 20 to get better resolution, the bubble sort turned out to be
faster: 3 seconds vs. 4 seconds.

So, I think you can see that you are not sacrificing as much performance
using the bubble sort with small data sets as you thought.

In terms of memory resources, I have to think that the array will have a
much smaller "footprint" than the recordset, so fewer server recources will
be used.

Here is the code:
********************************************************************
<%@ Language=VBScript %>
<%
option explicit
dim arFiles(),i,j,f,bjm,fc,fl,drive,z
dim t, SortType
dim tmpName, tmpDate


set bjm= CreateObject("Scripting.FileSystemObject")

drive = Server.MapPath("CurrentUsers")


set f = bjm.GetFolder(drive)
set fc = f.files

Response.Write "<table><tr><th align=center>Bubble Sort</th>" & _
"<th align=center>Recordset Sort</th></tr><tr><td>"
t = now()
for z = 1 to 10
redim arFiles(1,fc.count - 1)
i = 0
for each fl in fc


arFiles(0,i) = fl.name
'assuming you want the last-modified date
arFiles(1,i) = fl.DateLastModified

i = i + 1


next
'now sort by date:
for i = 0 to ubound(arFiles,2) - 1
for j = i+1 to ubound(arFiles,2)
if cdate(arfiles(1,i)) > cdate(arfiles(1,j)) then
'swap them
tmpName = arFiles(0,j)
tmpDate = arFiles(1,j)
arFiles(0,j) = arfiles(0,i)
arFiles(1,j) = arfiles(1,i)
arfiles(0,i) = tmpName
arfiles(1,i) = tmpDate
end if
next
next
next
'now write the results

Response.Write "<TABLE>"


for i = 0 to ubound(arFiles,2)

Response.Write "<tr><td><a href='/" & arFiles(0,i) & "'>"
Response.Write arfiles(0,i) & "</a></td></tr><p>"
next
Response.Write "</TABLE>"
Response.Write ubound(arFiles,2)+1 & " files - " & datediff("s",t,now()) & _
" seconds for " & z-1 & " re-sorts.</td><td>"
erase arFiles
dim rs
const adUseClient = 1
Const adDate = 7
Const adBSTR = 8
t=now()
for z = 1 to 10
set rs = Server.CreateObject("ADODB.Recordset")
rs.CursorLocation = adUseClient
With rs.Fields
.Append "filename", adBSTR, 128
.Append "ModDate", adDate
End With
rs.Open
for each fl in fc
rs.AddNew array(0,1),array(fl.name,fl.DateLastModified)
next
rs.Sort="modDate"
rs.MoveFirst
next
Response.Write "<TABLE>"
do until rs.EOF
Response.Write "<tr><td><a href='/" & rs(0) & "'>"
Response.Write rs(0) & "</a></td></tr><p>"
rs.MoveNext
loop
Response.Write "</TABLE>"
Response.Write rs.RecordCount & " files - " & datediff("s",t,now()) & _
" seconds for " & z-1 & " re-sorts.</td></tr></table>"
rs.Close
set rs = nothing
%>
********************************************************************

HTH,
Bob Barrows

Aaron Bertrand - MVP

unread,
May 23, 2003, 11:19:28 AM5/23/03
to
> In terms of memory resources, I have to think that the array will have a
> much smaller "footprint" than the recordset, so fewer server recources
will
> be used.

What, you didn't check? :-)


Bob Barrows

unread,
May 23, 2003, 11:40:34 AM5/23/03
to

:-)
I left that as an exercise for the reader.


@nothere.com James

unread,
May 23, 2003, 1:22:29 PM5/23/03
to
Cheers bob,

that's now working !!


>drive = Server.MapPath("../Backup")

Hopefully Last Question !!

I'm trying to copy and rename a file, this works fine if I copy
"\Test.txt" to "\Backup\NewTest.txt"...

But I'm trying to do this :

Copy Test.Txt from root to Backup but named as
"Todays Date Test.txt" !

I'm using :

objFSO.CopyFile (Server.MapPath("/Working.MDB")), (Server.MapPath("/Backup/
& Date() & backup.mdb")), OverwriteExisting

And I end up with a file called "Date()Backup.mdb"

How do Get this to be correct ??

Thanks

Aaron Bertrand - MVP

unread,
May 23, 2003, 1:31:17 PM5/23/03
to
> objFSO.CopyFile (Server.MapPath("/Working.MDB")),
(Server.MapPath("/Backup/
> & Date() & backup.mdb")), OverwriteExisting
>
> And I end up with a file called "Date()Backup.mdb"

Yes, because you've put Date() in the string.

@nothere.com James

unread,
May 23, 2003, 1:44:55 PM5/23/03
to
On Fri, 23 May 2003 13:31:17 -0400, "Aaron Bertrand - MVP"
<aa...@TRASHaspfaq.com> wrote:

>Yes, because you've put Date() in the string.
>
>objFSO.CopyFile (Server.MapPath("/Working.MDB")), (Server.MapPath("/Backup/"
>& Date() & "backup.mdb")), OverwriteExisting


This causes me to get a path not found error !!

The full code i'm using is :

<%
Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FileExists (Server.MapPath("/Working.MDB")) Then

Const OverwriteExisting = True

objFSO.CopyFile (Server.MapPath("/Working.MDB")),(Server.MapPath("/Backup/"


& Date() & "backup.mdb")), OverwriteExisting

%>

the file working.mdb exists in the root, and there is a folder called
backup off the root !

Help !! Thanks

Bob Barrows

unread,
May 23, 2003, 2:08:56 PM5/23/03
to
Do this:
dim sNewFile
sNewFile = "/Backup/" & Date() & "backup.mdb"
response.write sNewFile
sNewFile = server.mappath(sNewFile)
response.write sNewFile

You should see the problem here, right?

I suspect the "/" 's in the date are being added to the path being fed to
MapPath. What you need to do is at least this:
sNewFile = Server.MapPath("/Backup/") & "\" & Date() & "backup.mdb"
response.write sNewFile

However, the "/" 's may still be a problem. You may need to use Replace to
get rid of them entirely

HTH,
Bob Barrows

@nothere.com James

unread,
May 23, 2003, 6:48:58 PM5/23/03
to

Hi Bob,

Thanks.. Now I understand..

I have rewritten the code and now it works !!

dim sNewFile
dim bNewFile
dim nDate
dim fDate

nDate = Date()
fDate = replace(nDate,"/","-")
sNewFile = fDate & " - Backup.mbd"

Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists (Server.MapPath("/Working.MDB")) Then
Const OverwriteExisting = True

objFSO.CopyFile (Server.MapPath("/Working.MDB")), Server.MapPath("/Backup/"
& sNewFile)

This creates me a file called : 23-05-03 - Backup.MDB

Hmm... Now Do I add the time as well !!!

Thanks


Bob Barrows

unread,
May 23, 2003, 8:55:03 PM5/23/03
to
James wrote:
> Hi Bob,
>
> Thanks.. Now I understand..
>
> Hmm... Now Do I add the time as well !!!
>
> Thanks

Use Now() instead of Date(). Only you will have to deal with the colons ...

Bob


0 new messages