ASP 使用FSO读取文件 可以按照文件名和修改日期排序
下面为mss.asp的代码
<body>
<%
'FSO读取文件
Function filelist(path1)
Dim oFSO, oCurrentFolder, oFolders, oFolder, oFiles, oFile ,path,pf
Set oFSO = Server.CreateObject( "Scripting.FileSystemObject" )
path =path1
Set oCurrentFolder = oFSO.GetFolder(path)
Set oFolders = oCurrentFolder.SubFolders
Set oFiles = oCurrentFolder.Files
for each oFolders in oFolders
'列印文件夹
response.write "<a href=""mss.asp?path1="&server.urlencode(path1)&""&server.urlencode("/")&""&server.urlencode(oFolders.name)&""" target=_self>"&oFolders.name&"</a><br>"
next
response.write "<hr>"
'处理文件
Dim tmpDateStr,tmpNameStr,tmpDates,tmpNames,td,tn
Dim i,j,k,f,fil
tmpDateStr = ""
tmpNameStr = ""
For Each oFile in oFiles
'读取文件修改日期,并格式化(yyyy/MM/dd HH:mm:ss)
fil =oFile.DateLastModified
f =year(fil) &"/"&right("0"&month(fil),2)&"/"&right("0"&day(fil),2)&" "&right("0"&hour(fil),2)&":"&right("0"&minute(fil),2)&":"&right("0"&second(fil),2)
'读取文件到数组
tmpDateStr = tmpDateStr & f & "|"
tmpNameStr = tmpNameStr & oFile.name & "|"
Next
tmpDates = Split(tmpDateStr,"|")
tmpNames = Split(tmpNameStr,"|")
k = UBound(tmpDates)
dim tmptotal()
if k<0 then
k=0
end if
redim tmptotal(k,2)
for t=0 to k-1
if isempty(request("id")) then
tmptotal(t,0)=tmpDates(t)
tmptotal(t,1)=tmpNames(t)
else
if request("id")=0 then
tmptotal(t,0)=tmpDates(t)
tmptotal(t,1)=tmpNames(t)
else
tmptotal(t,0)=tmpNames(t)
tmptotal(t,1)=tmpDates(t)
end if
end if
next
'排序
For i=0 To k
For j=0 To k
If j=k Then Exit For
If tmptotal(j,1)<tmptotal(j+1,1) Then
td = tmptotal(j,1)
tn = tmptotal(j,0)
tmptotal(j,1) = tmptotal(j+1,1)
tmptotal(j,0) = tmptotal(j+1,0)
tmptotal(j+1,1) = td
tmptotal(j+1,0) = tn
End If
Next
Next
filelist=tmptotal
end Function
Dim fo
'根据返回值决定文件路径
if IsEmpty(Request("path1"))then
fo ="C:/Inet/sign"
else
fo =Request("path1")
end if
'列印文件路径
response.write("<font size=6>--"&right(fo,len(fo)-7-instr(fo,"sign"))&"</font><br><hr>")
'排除文件路径最后'/'
dim st,g,fo1
fo1=""
st=split(fo,"/")
g=ubound(st)-1
for s=0 to g
fo1=fo1 & st(s)&"/"
next
if right(fo1,1)="/" then
fo1 =left(fo1,len(fo1)-1)
end if
'允许访问到的文件夹最高层次
if fo1<>"C:/Inet" then
response.write "<a href=""mss.asp?path1="&fo1&""" target=_self>[To Parent Directory]</a>"
end if
%>
<script language="javaScript" type="text/JavaScript">
<!--
//排序方式选择事件
function Showdiv(fo1)
{
if (di[0].checked)
{
di[0].checked=true
window.location.href='mss.asp?id=0&path1='+fo1
}
else
{
di[1].checked=true
window.location.href='mss.asp?id=1&path1='+fo1
}
}
//-->
'server.urlencode用于处理有可能出现特殊符号
</script>
<p align="left">Desc:
<INPUT id="name1" name="di" type="radio" value="0" <%if isempty(request("id")) then response.write "checked=""checked""" else if request("id")=0 then response.write"checked=""checked""" end if end if %> onclick="return Showdiv('<%=server.urlencode(fo)%>')" >By Name<INPUT id="date1" name="di" type="radio" value="1" <%if isempty(request("id")) then response.write"" else if request("id")=1 then response.write"checked=""checked""" end if end if %> onclick="return Showdiv('<%=server.urlencode(fo)%>')">By Date
</p>
<%
'列印文件
filearr =filelist(""&fo&"")
For i=0 To ubound(filearr)-1
if isempty(request("id")) then
Response.Write(filearr(i,0) & " -- <a href=""download.asp?filename="&server.urlencode(fo)&""&server.urlencode("/")&""&server.urlencode(filearr(i,1))&""">"&filearr(i,1)&"</a><br>")
else
if request("id")=0 then
Response.Write(filearr(i,0) & " -- <a href=""download.asp?filename="&server.urlencode(fo)&""&server.urlencode("/")&""&server.urlencode(filearr(i,1))&""">"&filearr(i,1)&"</a><br>")
else
Response.Write(filearr(i,1) & " -- <a href=""download.asp?filename="&server.urlencode(fo)&""&server.urlencode("/")&""&server.urlencode(filearr(i,0))&""">"&filearr(i,0)&"</a><br>")
end if
end if
Next
%>
</body>


浙公网安备 33010602011771号