ASP文件操作--列出目录及文件
网络整理 - 09-11
把以下文件建在同一目录,IIS中确定文件操作组件可用,即可用ASP列出文件和目录了,呵呵欢迎光临我的主页 ----文件folder.inc-----
<%
folderini="c:\"
%>
----文件index.asp-----
<!--#Include file="folder.inc"-->
<html>
<head>
<title>FileSearch</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<frameset rows="80,*" frameborder="1" framespacing="1" cols="*">
<frame scrolling="NO" src="up.asp" >
<frame src="getinfo.asp?foldinfo=<%=folderini%>">
</frameset>
<noframes><body bgcolor="#FFFFFF">
</body></noframes>
</html>
----文件up.asp-----
<!--#Include file="folder.inc"-->
<html><title>Up</title>
<head>
<style type="text/css">
<!--
.ttl1_txt { font-family: "Verdana", "Arial", "Helvetica", "sans-serif"; font-size: 9pt}
.cnt_txt { font-family: "Verdana", "Arial", "Helvetica", "sans-serif"; font-size: 9pt}
A:link{color:#0000ff;text-decoration: none}
A:visited {color:#0000ff;text-decoration: none}
A:active {color:#33ccff;text-decoration: underline}
A:hover {color:#33ccff;text-decoration: underline}
body{font-family: "Verdana", "Arial", "Helvetica", "sans-serif"; font-size: 9pt}
Tr{font-family: "Verdana", "Arial", "Helvetica", "sans-serif"; font-size: 9pt}
TD{font-family: "Verdana", "Arial", "Helvetica", "sans-serif"; font-size: 9pt
-->
</style>
</head>
<table cellspacing="1" cellpadding="1">
<tr>
<td><a href="getinfo.asp?foldinfo=<%=folderini%>" target="mainFrame">List All Folder/File Info</a>
</td>
</tr>
</table>
</html>
----文件getinfo.asp-----
<!--#Include file="folder.inc"-->
<%foldinfo=trim(Request.Querystring("foldinfo"))%>
<html>
<head>
<title>GetInfo</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
.ttl1_txt { font-family: "Verdana", "Arial", "Helvetica", "sans-serif"; font-size: 9pt}
.cnt_txt { font-family: "Verdana", "Arial", "Helvetica", "sans-serif"; font-size: 9pt}
A:link{color:#0000ff;text-decoration: none}
A:visited {color:#0000ff;text-decoration: none}
A:active {color:#33ccff;text-decoration: underline}
A:hover {color:#33ccff;text-decoration: underline}
body{font-family: "Verdana", "Arial", "Helvetica", "sans-serif"; font-size: 9pt}
Tr{font-family: "Verdana", "Arial", "Helvetica", "sans-serif"; font-size: 9pt}
TD{font-family: "Verdana", "Arial", "Helvetica", "sans-serif"; font-size: 9pt
-->
</style>
<script Language="Javascript">
function fileinfo(filenamecode,filesize,lastmodify){
//document.write(filenamecode);
window.open("fileinfo.asp?filename="+filenamecode+"&filesize="+filesize+"&lastmodify="+lastmodify,"","height=200,width=450,resizable=yes,scrollbars=yes,status=yes,toolbar=no,menubar=no,location=no");
}
</script>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<table cellspacing="0" cellpadding="0">
<tr>
<td> <font color="#000000">Current Folder:--</font><font color="#FF7120"><%=foldinfo%></font></td>
</tr>
<tr>
<td>
<hr size="1">
</td>
</tr>
<tr>
<td>
<table cellspacing="1" cellpadding="1">
<tr bgcolor="#00CC00">
<td>Folder Name:</td>
<td>Size:</td>
<td>LastModify:</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<%
upfolder=left(foldinfo,len(foldinfo)-1)
upfolder=left(upfolder,InstrRev(upfolder, "\"))
if foldinfo<>folderini then
response.write("<a href='getinfo.asp?foldinfo="&upfolder&"'>Parent Folder</a>")
else
response.write("Parent Folder")
end if%>
</td>
</tr>
<tr>
<td>
<% ShowFolderList(foldinfo) %>
</td>
</tr>
<tr>
<td>
<table cellspacing="1" cellpadding="1">
<tr bgcolor="#009999">
<td>File Name:</td>
<td>Size:</td>
<td>LastModify:</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<% showfolderinfo(foldinfo)%>
</td>
</tr>
<tr>
<td> </td>
</tr>
</table>
<%
Sub ShowFolderList(folderspec)
Dim fs, f, f1, fc, s, schild,p,fsize
Set fs = CreateObject("scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)
Set fc = f.SubFolders
For Each f1 in fc
s = f1.name
's = s & vbCrLf
p = f1.DateLastModified
fsize = f1.Size
schild=folderspec&s&"\"
Response.write("<table cellspacing='1' cellpadding='1'>")
Response.write("<tr>")
Response.write("<td bgcolor='#ECFFD9'><font face='Wingdings' font size='3pt'>0</font><a href='getinfo.asp?foldinfo="&schild&"'>"&s&"</a></td>")
Response.write("<td bgcolor='#ECFFD9'>"&fsize&"</td>")
Response.write("<td bgcolor='#ECFFD9'>"&p&"</td>")
Response.write("</tr>")
Response.write("</table>")
Next
End Sub
Sub showfolderinfo(folderspc)
set MyFileObject=Server.CreateObject("scripting.FileSystemObject")
Set MyFolder=MyFileObject.GetFolder(folderspc)
for each thing in MyFolder.Files
'Response.write("<br>"&thing)
'===============Get file information
Set afile=MyFileObject.GetFile(thing)
'==========filename correct code
filenamecode=afile.name
filedetail=folderspc+filenamecode
filedetail=replace(filedetail,"\","*s_p_l_i_t*")
filesize=afile.size
lastmodify=afile.DateLastModified
Response.write("<table cellspacing='1' cellpadding='1'>")
Response.write("<tr>")
Response.write("<td bgcolor='#f4f4ff'><font face='Wingdings' font size='3pt'><</font><a href="&chr(34)&"javascript:fileinfo('"&filedetail&"','"&filesize&"','"&lastmodify&"')"&chr(34)&">"&filenamecode&"</a></td>")
Response.write("<td bgcolor='#f4f4ff'>"&filesize&"</td>")
Response.write("<td bgcolor='#f4f4ff'>"&lastmodify&"</td>")
Response.write("</tr>")
Response.write("</table>")
Next
End sub
%>
</body>
</html>
----文件fileinfo.asp-----
<%
filename=Request.QueryString("filename")
filename=replace(filename,"*s_p_l_i_t*","\")
filesize=Request.QueryString("filesize")
lastmodify=Request.QueryString("lastmodify")
%>
<html>
<head>
<style type="text/css">
<!--
.ttl1_txt { font-family: "Verdana", "Arial", "Helvetica", "sans-serif"; font-size: 9pt}
.cnt_txt { font-family: "Verdana", "Arial", "Helvetica", "sans-serif"; font-size: 9pt}
A:link{color:#0000ff;text-decoration: none}
A:visited {color:#0000ff;text-decoration: none}
A:active {color:#33ccff;text-decoration: underline}
A:hover {color:#33ccff;text-decoration: underline}
body{font-family: "Verdana", "Arial", "Helvetica", "sans-serif"; font-size: 9pt}
Tr{font-family: "Verdana", "Arial", "Helvetica", "sans-serif"; font-size: 9pt}
TD{font-family: "Verdana", "Arial", "Helvetica", "sans-serif"; font-size: 9pt
-->
</style>
</head>
<body>
<%
'Response.write filename&"<br>"&filesize&"<br>"&lastmodify
%>
<table cellspacing="0" cellpadding="0">
<tr>
<td bgcolor="#00CC00" colspan="2">Current File Info:</td>
</tr>
<tr valign="top">
<td>File Location:</td>
<td><font color="#990000"><font face='Wingdings' font size='3pt'><</font><b> <%=filename%></b></font></td>
</tr>
<tr>
<td colspan="2">
<hr size="1">
</td>
</tr>
<tr valign="top">
<td>File Size: </td>
<td><%=filesize%> bytes</td>
</tr>
<tr>
<td colspan="2">
<hr size="1">
</td>
</tr>
<tr valign="top">
<td>LastModify: </td>
<td><%=lastmodify%></td>
</tr>
<tr>
<td colspan="2">
<hr size="1">
</td>
</tr>
<tr>
<td colspan="2">
<p>Please Use appropriate Application to Open this File.</p>
</td>
</tr>
<tr>
<td colspan="2"><a href="javascript:this.window.close()">Close
Window</a></td>
</tr>
</table>
</body>
</html>