Link to home
Start Free TrialLog in
Avatar of ottob
ottob

asked on

ASP Array To JS Sort() & Back

I Have This ASP Page That Reads Recreates html On THe Fly Of A Directory Structure With Links That Call Upon Itself To Navigate All Subfolders And Files.

Problem Is, It Doesn't Sort Alphabetically

I'm Thinking JScript's Sort();
But I Need To Send The Array To JS And Back Again.
And I Don't Know How.
Please Help.
Here's The Code:

<%
response.write "<body bgcolor='#d3d3d3' text='#000000' link='#FF0000' vlink='#0000FF'>"
' newpath will be the directory name passed in the URL when we
' link to another directory
newpath = request.querystring("newpath")
folderspec = "/"
' we want to add a forward slash to the new path if the
' value of newpath isn't a zero-length string
if  newpath<>"" then
    folderspec = folderspec & newpath & "/"
end if
' now we will create our filesystem object
Set fs = CreateObject("Scripting.FileSystemObject")
' now we will define the folder and subfolders
Set folder = fs.GetFolder(server.mappath(folderspec))
Set subfolder = folder.subfolders
' let's return some HTML code here
response.write "<p><font face='arial' size='4'><strong>DIRECTORY</strong>&nbsp;"
' Now we've got to take the path represented by the variable
' newpath and trim off the last subdirectory so we can use that
' as a link to the directory above it. We will do that first
' checking if there is, indeed, a value for newpath, and if there
' is, we will search for a forward slash in the string, starting
' from the end of the string, and use that position as a pointer
' so we know how many characters to peel off of the string "newpath".
' Since the pointer is at the position of the forward slash and we
' don't want the forward slash to remain, we'll subtract 1 from it.
If newpath="" then
response.write "<br>"
end if
If newpath<>"" then
   Pointer = instrrev(newpath,"/")
' if the forward slash is found, we'll create the link to the
' directory above the current one.
    If Pointer>0 then
        UpOneLevel = Left(newpath,Pointer-1)
        response.write "<a href='" & request.servervariables("script_name")
        response.write "?newpath=" & UpOneLevel & "'><img src='up.gif' border='0'></a></p>"
' Now let's make provisions where the forward slash isn't found
    Else
        response.write "<a href='" & request.servervariables("script_name")
        response.write "'><img src='up.gif' border='0'></a></p>"
    End If
End If
' Now we can go through and add links to all the subfolders
For Each newfolder in subfolder
if newfolder.name = "Recycled" then
  arttem="t"
else
    response.write "<A HREF='"
    response.write request.servervariables("script_name")
' This is where I ran into problems...if newpath isn't a zero-length
' string, we won't want to add a forward slash, but we do want to add
' it if the value of newpath isn't a zero-length string
    If newpath="" then
        response.write "?newpath=" & newpath & newfolder.name
    Else
        response.write "?newpath=" & newpath & "/" & newfolder.name
    End If
    response.write "'>"
    response.write newfolder.name
    response.write "</A><br>"
end if
Next
' Ok, time to go grab those files in the current directory
' First we'll do a bit of HTML
response.write "<p><strong>Current directory = \" & folder.name & "</strong></p>"

Set filelist = folder.files
' iterate through the files and make them links
For Each file in filelist
    response.write "<A HREF='"
    response.write folderspec & file.name
    response.write "'>"
    response.write file.name & " - " & datevalue(file.datecreated) & " - " & file.size & " BYTES"
    response.write "</A><br>"
Next
' Now let's clean up after ourselves - destroy those objects
set folder=nothing
set subfolder=nothing
set filelist=nothing
set fs=nothing
' a little bit more HTML formatting and we're done!
 response.write "</font></body>"
%>
Avatar of ottob
ottob

ASKER

The Script In Action
http://24.187.1.18/
Actually the code that describes exactly what I think you want to do can be found at:

http://www.asp101.com/samples/viewasp.asp?file=dir%5Flist%5Fsort%2Easp

-matty
Actually this is the main link for the sample:

http://www.asp101.com/samples/dir_list_sort.asp

-matty
Avatar of ottob

ASKER

I've Seen Those In My Own Search But,
I Don't Think Thats What I'm Looking For.
That Suposedly List File/Folders In Order Of File Extention
I Want Folers Listed Alphabeticaly And Then Files Listed Alphabeticaly
The Sample A Gave In My Last Comment Lists Folders And Then Files But Not Alphabeticaly
Ferthermore I Couldn't Get That Page To Work For Me So I Can't Really See Its Results.
Avatar of ottob

ASKER

That File Is:
http://24.187.1.18/test.asp
As My Testing It Out
I Get Errors

Avatar of ottob

ASKER

OK I Got It TO Work Now Let Me Play With That A Wile Thanks
I'll Let You Know
Avatar of ottob

ASKER

Nope Still Not Sorted Alphabeticaly
Avatar of ottob

ASKER

Also That Sample Dosn't Work Through Subdirectories
https://www.experts-exchange.com/jsp/qShow.jsp?ta=asp&qid=20147188

I posted code on that question that will do probably exactly what you want. And if not, could be changed to do what you want.
Avatar of ottob

ASKER

Not Quite
The Code I Provided Is More Complex Than You Are Both Showing Me
Look At My Code
Goto:
http://24.187.1.18/
Click A Link To A Folder And See The Results
Notice THe Links Are To The Same Asp But With A "?dir" After It. That Grabs Thats Directory's Folders And Files And Reads Then Into A New Page.

AND!!!!!!!!!!!!!!!
Niether Of The 2 Samples Did What I'm Looking For.
SIMPLY PUT.
I Want What I Have At:
http://24.187.1.18/Dir6.asp
(The Code For This Is Suplied In Question)
I Just Want It Sorted In ALPHABETICAL ORDER!!!
Avatar of ottob

ASKER

Not Quite
The Code I Provided Is More Complex Than You Are Both Showing Me
Look At My Code
Goto:
http://24.187.1.18/
Click A Link To A Folder And See The Results
Notice THe Links Are To The Same Asp But With A "?dir" After It. That Grabs Thats Directory's Folders And Files And Reads Then Into A New Page.

AND!!!!!!!!!!!!!!!
Niether Of The 2 Samples Did What I'm Looking For.
SIMPLY PUT.
I Want What I Have At:
http://24.187.1.18/Dir6.asp
(The Code For This Is Suplied In Question)
I Just Want It Sorted In ALPHABETICAL ORDER!!!
ASKER CERTIFIED SOLUTION
Avatar of mattyk
mattyk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ottob

ASKER

Same As Other
Avatar of ottob

ASKER

JS Is Not Needed To Sort Alphabeticaly
Both mattyk And phuctran's Efforts Came Up With The Following Working Code:

<%@ Language=VBSCRIPT %>
<%
Response.Buffer = TRUE
Response.Expires = 0
Response.ExpiresAbsolute = Now - 1
%>

<%
response.write "<body bgcolor='#d3d3d3' text='#000000' link='#FF0000' vlink='#0000FF'>"
' newpath will be the directory name passed in the URL when we
' link to another directory
newpath = request.querystring("newpath")
folderspec = "/"
' we want to add a forward slash to the new path if the
' value of newpath isn't a zero-length string
if  newpath<>"" then
 folderspec = folderspec & newpath & "/"
end if
' now we will create our filesystem object
Set fs = CreateObject("Scripting.FileSystemObject")
' now we will define the folder and subfolders
Set folder = fs.GetFolder(server.mappath(folderspec))
Set subfolder = folder.subfolders
' let's return some HTML code here
response.write "<p><font face='arial' size='4'><strong>DIRECTORY</strong>&nbsp;"
' Now we've got to take the path represented by the variable
' newpath and trim off the last subdirectory so we can use that
' as a link to the directory above it. We will do that first
' checking if there is, indeed, a value for newpath, and if there
' is, we will search for a forward slash in the string, starting
' from the end of the string, and use that position as a pointer
' so we know how many characters to peel off of the string "newpath".
' Since the pointer is at the position of the forward slash and we
' don't want the forward slash to remain, we'll subtract 1 from it.
If newpath="" then
response.write "<br>"
end if
If newpath<>"" then
Pointer = instrrev(newpath,"/")
' if the forward slash is found, we'll create the link to the
' directory above the current one.
 If Pointer>0 then
     UpOneLevel = Left(newpath,Pointer-1)
     response.write "<a href='" & request.servervariables("script_name")
     response.write "?newpath=" & UpOneLevel & "'><img src='up.gif' border='0'></a></p>"
' Now let's make provisions where the forward slash isn't found
 Else
     response.write "<a href='" & request.servervariables("script_name")
     response.write "'><img src='up.gif' border='0'></a></p>"
 End If
End If
' Now we can go through and add links to all the subfolders
'*********ADDED BY MATTY
 Dim rstFolders
 Set rstFolders = Server.CreateObject("ADODB.Recordset")
 rstFolders.Fields.Append "name", 200, 255
 rstFolders.Fields.Append "date", 7
 rstFolders.Open

 For Each newfolder in subfolder
   if newfolder.name = "Recycled" then
     arttem="t"
   else
     rstFolders.AddNew
     rstFolders.Fields("name").Value = newfolder.Name
   End If
 Next
 
 rstFolders.Sort = "name ASC"
 
 If objItem > 0 Then
   rstFolders.MoveFirst
 End If
 
 Do While Not rstFolders.EOF
   response.write "<A HREF='"
   response.write request.servervariables("script_name")
   ' This is where I ran into problems...if newpath isn't a zero-length
   ' string, we won't want to add a forward slash, but we do want to add
   ' it if the value of newpath isn't a zero-length string
   If newpath="" then
       response.write "?newpath=" & newpath & rstFolders.Fields("name").Value
   Else
       response.write "?newpath=" & newpath & "/" & rstFolders.Fields("name").Value
   End If
   response.write "'>"
   response.write rstFolders.Fields("name").Value
   response.write "</A><br>"

   rstFolders.MoveNext
 Loop

' Ok, time to go grab those files in the current directory
' First we'll do a bit of HTML
response.write "<p><strong>Current directory = \" & folder.name & "</strong></p>"

Set filelist = folder.files
' iterate through the files and make them links

'*********ADDED BY MATTY
 Set rstFiles = Server.CreateObject("ADODB.Recordset")
 rstFiles.Fields.Append "name", 200, 255
 rstFiles.Fields.Append "size", 3
 rstFiles.Fields.Append "date", 7
 rstFiles.Fields.Append "type", 200, 255
 rstFiles.Open
 
 For Each objItem In filelist
      rstFiles.AddNew
      rstFiles.Fields("name").Value = objItem.Name
      rstFiles.Fields("size").Value = objItem.Size
      rstFiles.Fields("date").Value = objItem.DateCreated
      rstFiles.Fields("type").Value = objItem.Type
 Next
 
 rstFiles.Sort = "name ASC"
 
 If objItem > 0 Then
   rstFiles.MoveFirst
 End If
 
 Do While Not rstFiles.EOF
   response.write "<A HREF='"
   response.write folderspec & rstFiles.Fields("name").Value
   response.write "'>"
   response.write rstFiles.Fields("name").Value & " - " & datevalue(rstFiles.Fields("date").Value)
& " - " & rstFiles.Fields("size").Value & " BYTES"
   response.write "</A><br>"
   rstFiles.MoveNext
 Loop

'***************************Finish Edit

' Now let's clean up after ourselves - destroy those objects
rstFolders.Close
set rstFolders = Nothing
rstFiles.Close
set rstFiles = Nothing
set folder=nothing
set subfolder=nothing
set filelist=nothing
set fs=nothing
' a little bit more HTML formatting and we're done!
response.write "</font></body>"
%>