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
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.
"James" <James @ nothere.com> wrote in message
news:f1fqcvkc6fd1m1n2l...@4ax.com...
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 :)
--
>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
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
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...
Bubble sort in Vbscript?? Ha! Just go for a CPU upgrade :)
--
> Bob Barrows
>
>
Someday I'll run a comparison ... :-)
Bob
Um, I take it you didn't read the whole article, Egbert.
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
What, you didn't check? :-)
:-)
I left that as an exercise for the reader.
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
Yes, because you've put Date() in the string.
>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
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
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
Use Now() instead of Date(). Only you will have to deal with the colons ...
Bob