VB icon

File Listing Sorter

Email
Submitted on: 1/6/2015 10:47:00 AM
By: Oliver French (from psc cd)  
Level: Intermediate
User Rating: By 3 Users
Compatibility: ASP (Active Server Pages), HTML, VbScript (browser/client side)
Views: 831
 
     This shows how to display files in a folder sorted by parameter (name,size date). You can sort in either direction (ascending or descending).

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: File Listing Sorter
' Description:This shows how to display files in a folder sorted by parameter (name,size date). You can sort in either direction (ascending or descending).
' By: Oliver French (from psc cd)
'
' Inputs:The sorting function takes the array to sort, the direction to sort, and the index number of the first dimension to sort by.
'
' Returns:The sorting function returns a sorted array.
'
' Assumes:Sort array is similar to Andy Slowey's sort array function but adapted to do multi-dimensional.
'**************************************

<%@ Language=VBScript %>
<% option explicit %>
<% Response.Buffer = True %>
<%
Dim sSort
Dim FileArray() ' (0 is name, 1 is size, and 2 is last modified) 
dim objFSO
dim objFile
dim objFolder 
dim sMapPath
dim sUrlPath
Dim filename, filecollection, filesize, modified
Dim i
Dim FinalFileArray
' Make path strings (change this to the path of the folder you want to see)
sUrlPath = "iDesign/pages/Sample Files"
sMapPath = Server.MapPath(sUrlPath)
' Create FSO object and buddies
set objFSO = createobject("Scripting.FileSystemObject")
set objFolder = objFSO.GetFolder(sMapPath)
Set filecollection = objFolder.Files
' Resize file array to appropriate number of files to hold (with 3 attributes to hold
Redim FileArray(2,filecollection.count - 1)
Redim FinalFileArray(2,filecollection.count - 1)
' Load file data into array
i = 0
For Each objFile In objFolder.Files
	filename=right(objFile.name,len(objFile.name)-InStrRev(objFile.name, "\"))
	filesize = objFile.size
	modified = objFile.DateLastModified
	FileArray(0, i) = filename
	FileArray(1, i) = filesize
	FileArray(2, i) = modified
	i = i + 1
Next
' Sort array according to method
sSort = Request("Sort")
Select case sSort
	Case "Name"
		FinalFileArray = SortAlpha(FileArray,"DESC",0)
	Case "Size"
		FinalFileArray = SortAlpha(FileArray,"DESC",1)
	Case "Modified"
		FinalFileArray = SortAlpha(FileArray,"DESC",2)
End Select
' ***********************************************************
' ***********************************************************
' **	2-D sorting function 							 **
' ***********************************************************
' ***********************************************************
Function SortAlpha(ary, direction, indexnum)
	Dim StopWork
	Dim i
	dim i2
	Dim firstval()
	Dim secondval()
	
	redim firstval(ubound(ary,1))
	redim secondval(ubound(ary,1))
		
	StopWork=False
	Do Until StopWork=True
		StopWork=True
		For i = 0 To UBound(ary,2)
			if i=UBound(ary,2) Then Exit For
			if UCase(Direction) = "DESC" Then
				if ary(indexnum,i) < ary(indexnum,i+1) Then
					For i2 = 0 to ubound(firstval)
						firstval(i2) = ary(i2,i)
						secondval(i2) = ary(i2,i+1)
						ary(i2,i) = secondval(i2)
						ary(i2,i+1) = firstval(i2)									
					Next
					StopWork=False
				End if
			Else
				if ary(indexnum,i) > ary(indexnum,i+1) Then
					For i2 = 0 to ubound(firstval)
						firstval(i2) = ary(i2,i)
						secondval(i2) = ary(i2,i+1)
						ary(i2,i) = secondval(i2)
						ary(i2, i+1) = firstval(i2)					
					Next
					StopWork=False
				End if
			End if
		Next
	Loop
	SortAlpha=ary
	
End Function
%>
<html>
<head>
<title></title>
</head>
<body>
<table width="75%" border="2" cellspacing="0" cellpadding="2" align="center" bgcolor="#FFFFCC" bordercolor="#000000">
 <tr> 
 <td> <!-- The name of this particular file was Directory.asp -->
 <div align="center"><b><a href="Directory.asp?Sort=Name">Name</a></b></div>
 </td>
 <td> 
 <div align="center"><b><a href="Directory.asp?Sort=Size">Size</a></b></div>
 </td>
 <td> 
 <div align="center"><b><a href="Directory.asp?Sort=Modified">Modified</a></b></div>
 </td>
 </tr>
<% 
 
	For i = 0 to Ubound(FinalFileArray,2)
		filename=FinalFileArray(0,i)
		filesize = FinalFileArray(1,i)
		modified = FinalFileArray(2,i)
		Response.Write "<tr><td>" & FileName & "</td><td>" & filesize & "</td><td>" & modified & "</td></tr>"		
	Next
%>
</table>
</body>
</html>


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.