Sort your files by ARRAY

Source: Internet
Author: User
Tags sorted by name

When you use the FileSystemObject object to obtain the file list in a directory, have you found that they cannot be sorted by name, extension, or file size, let's try to sort them by array.

If you want to sort by name, it will be very simple, but if you want to sort by file size or file creation time, it will be a bit troublesome. We will do this through a two-dimensional array.

The followingCodeDemonstrate how to achieve our goal by selecting the sort method, click Sort, and click twice to reverse the sorting.

<HTML>
<Head>
<Title> File Sorting demonstration </title>
</Head>
<Body>
<%
'Set a demo directory ,:)
Const directory = "/"
'Use constants to define the sorting method
Const file_name = 0' sort by name ...... And so on
Const file_ext = 1
Const file_type = 2
Const file_size = 3
Const file_created = 4
Const file_modified = 5
Const file_accessed = 6
'Obtain the sorting command. By default, the command is sorted by name.
Req = request ("sortby ")
If Len (req) <1 then sortby = 0 else sortby = CINT (req)
Req = request ("priorsort ")
If Len (req) <1 then priorsort =-1 else priorsort = CINT (req)
'Set Reverse Order
If sortby = priorsort then
Reverse = true
Priorsort =-1
Else
Reverse = false
Priorsort = sortby
End if
'Next let's start our real code...
Path = server. mappath (directory)
Set FSO = Createobject ("scripting. FileSystemObject ")
Set thecurrentfolder = FSO. getfolder (PATH)
Set curfiles = thecurrentfolder. Files
'Create a loop for these files
Dim thefiles ()
Redim thefiles (500) 'I just set a size
Currentslot =-1 'start before first slot
'Put all the information about the file into the array.
For each fileitem in curfiles
Fname = fileitem. Name
Fext = Limit Rev (fname ,".")
If fext <1 then fext = "" else fext = mid (fname, fext + 1)
FTYPE = fileitem. Type
Fsize = fileitem. Size
Fcreate = fileitem. datecreated
Fmod = fileitem. datelastmodified
Faccess = fileitem. datelastaccessed
Currentslot = currentslot + 1
If currentslot> ubound (thefiles) then
Redim preserve thefiles (currentslot + 99)
End if
'Put in the array
Thefiles (currentslot) = array (fname, fext, FTYPE, fsize, fcreate, fmod, faccess)
Next
'It's all in the array. Start the next step.
Filecount = currentslot 'file count
Redim preserve thefiles (currentslot)
'Sort
'(8 indicates string)
If vartype (thefiles (0) (sortby) = 8 then
If reverse then kind = 1 else kind = 2' sort the characters
Else
If reverse then kind = 3 else kind = 4' number, time...
End if
For I = filecount to 0 step-1
MINMAX = thefiles (0) (sortby)
Minmaxslot = 0
For j = 1 to I
Select case kind
Case 1
Mark = (strcomp (thefiles (j) (sortby), MINMAX, vbtextcompare) <0)
Case 2
Mark = (strcomp (thefiles (j) (sortby), MINMAX, vbtextcompare)> 0)
Case 3
Mark = (thefiles (j) (sortby) <MINMAX)
Case 4
Mark = (thefiles (j) (sortby)> MINMAX)
End select
If Mark then
MINMAX = thefiles (j) (sortby)
Minmaxslot = J
End if
Next
If minmaxslot <> I then
Temp = thefiles (minmaxslot)
Thefiles (minmaxslot) = thefiles (I)
Thefiles (I) = temp
End if
Next
'End
%>
<Form name = "dosort" method = "get">
<Input type = hidden name = priorsort value = "<% = priorsort %>">
<Input type = hidden name = sortby value = "-1">
</Form>
<Script language = "JavaScript">
Function resort (which)
{
Document. dosort. sortby. value = which;
Document. dosort. Submit ();
}
</SCRIPT>
<Center>
<Font size = "+ 2">
Show <% = (filecount + 1) %> files in this directory <% = PATH %>
</Font>
<P>
Click sort and then click reverse sort.
<P>
<Table border = 1 cellpadding = 3>
<Tr>
<TH> <a href = "javascript: resort (0);"> file name </a> </Th>
<TH> <a href = "javascript: resort (1);"> extension </a> </Th>
<TH> <a href = "javascript: resort (2);"> type </a> </Th>
<TH> <a href = "javascript: resort (3);"> size </a> </Th>
<TH> <a href = "javascript: resort (4);"> creation time </a> </Th>
<TH> <a href = "javascript: resort (5);"> last modification time </a> </Th>
<TH> <a href = "javascript: resort (6);"> last access time </a> </Th>
</Tr>
<%
For I = 0 to filecount
Response. Write "<tr>" & vbnewline
For J = 0 to ubound (thefiles (I ))
Response. Write "<TD>" & thefiles (I) (j) & "</TD>" & vbnewline
Next
Response. Write "</tr>" & vbnewline
Next
%>
</Table>
</Body>
</Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.