FSO操作示例(给初学者)
程序员文章站
2022-07-02 07:53:43
把下面代码存到一个asp文件中即可,方便管理服务器上的文件
http://bbs.asp2004.net 制作:小灰 qq:103895<...
把下面代码存到一个asp文件中即可,方便管理服务器上的文件
<title>http://bbs.asp2004.net 制作:小灰 qq:103895</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
a:link {
font-size: 9pt;
}
a:visited {
font-size: 9pt;
}
a:hover {
font-size: 9pt;
}
a:active {
font-size: 9pt;
}
body {
font-size: 9pt;
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
line-height: 20px;
}
td {
font-size: 9pt;
line-height: 20px;
}
-->
</style>
<%
action=request.querystring("action")
temp=split(request.servervariables("url"),"/")
url=temp(ubound(temp))
chklogin()
select case action
case "logout"
logout()
case "delfile"
delfile()
case "savefile"
savefile()
case "editfile"
fileedit()
case "newfile"
filenew()
case "upload"
upload()
case "saveupload"
saveupload()
case "delfolder"
delfolder()
case "savefolder"
savefolder()
case "editfolder"
editfolder()
case "newfolder"
newfolder()
case else
foldername=request.querystring("foldername")
filename=request.querystring("filename")
path=foldername
if path="" then path=server.mappath("./")
showfolderlist(path)
end select
function showfolderlist(folderspec)
temp=request.servervariables("http_referer")
temp=left(temp,instrrev(temp,"/"))
temp1=len(folderspec)-len(server.mappath("./"))-1
if temp1>0 then
temp1=right(folderspec,cint(temp1))
elseif temp1=-1 then
temp1=""
end if
tempurl=temp+replace(temp1,"\","/")+"/"
set fso = createobject("scripting.filesystemobject")
upfolderspec=fso.getparentfoldername(folderspec&"\")
%>
<a href="<%= url%>?action=newfolder&foldername=<%= folderspec%>">新建文件夹</a> | <a href="<%= url%>?action=newfile&filename=<%= folderspec&"\"%>">新建文件</a> | <a href="<%= url%>?action=upload">上传文件</a> | <a href="<%= url%>?foldername=<%= upfolderspec%>">向上</a> | <a href="file.asp">返回首页</a> | <a href="<%= url%>?action=logout">退出</a>
<table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#6595d6">
<tr bgcolor="#e4e8ef">
<td width="64%"><div align="center">名称</div></td>
<td width="7%"><div align="right">大小</div></td>
<td width="6%"><div align="center">类型</div></td>
<td width="15%"><div align="left">修改时间</div></td>
<td width="8%"><div align="center">操作</div></td>
</tr>
<%
'列出目录
set f = fso.getfolder(folderspec)
set fc = f.subfolders
for each f1 in fc
%>
<tr bgcolor="#e4e8ef" onmouseout=this.bgcolor='#e4e8ef'; onmouseover=this.bgcolor='#ffffff';>
<td><a href="<%= url%>?foldername=<%= folderspec%>\<%= f1.name%>"><%= f1.name%></a></td>
<td><div align="right"><%= f1.size%></div></td>
<td><div align="center">文件夹</div></td>
<td><div align="left"><%= f1.datelastmodified%></div></td>
<td><a href="<%= url%>?action=editfolder&foldername=<%= folderspec&"\"&f1.name%>">修改</a> <a href="<%= url%>?action=delfolder&foldername=<%= folderspec&"\"&f1.name%>" onclick="{if(confirm('删除文件夹<<%= f1.name%>>么?')){return true;}return false;}">删除</a></td>
</tr>
<%
next
'列出文件
set fc = f.files
for each f1 in fc
%>
<tr bgcolor="#e4e8ef" onmouseout=this.bgcolor='#e4e8ef'; onmouseover=this.bgcolor='#ffffff';>
<td><a href="<%= tempurl+f1.name%>" target="_blank"><%= f1.name%></a></td>
<td><div align="right"><%= f1.size%></div></td>
<td><div align="left"></div></td>
<td><div align="left"><%= f1.datelastmodified%></div></td>
<td><a href="<%= url%>?action=editfile&filename=<%= folderspec&"\"&f1.name%>">修改</a> <a href="<%= url%>?action=delfile&filename=<%= folderspec&"\"&f1.name%>" onclick="{if(confirm('删除文件<<%= f1.name%>>么?')){return true;}return false;}">删除</a></td>
</tr>
<%
next
set fso=nothing
%>
</table>
<%
end function
'文件操作
function delfile()
filename=request.querystring("filename")
set fso=server.createobject("scripting.filesystemobject")
fso.deletefile(filename)
tempurl=url&"?foldername="&fso.getparentfoldername(filename)
set fso=nothing
response.redirect(tempurl)
end function
function savefile()
filename=request.form("filename")
content=request.form("content")
filename1=request.form("filename1")
if request.servervariables("path_translated")<>filename then
set fso=server.createobject("scripting.filesystemobject")
set f1=fso.opentextfile(filename,2,true)
f1.write(content)
f1.close
if filename1<>"" and filename1<>filename then fso.deletefile(filename1)
tempurl=url&"?foldername="&fso.getparentfoldername(filename)
set fso=nothing
end if
response.redirect(tempurl)
end function
function filenew()
filename=request.querystring("filename")
content=""
fileform filename,content,filename1
end function
function fileedit()
filename=request.querystring("filename")
filename1=request.querystring("filename")
set fso=server.createobject("scripting.filesystemobject")
set f1=fso.opentextfile(filename,1,true)
content=server.htmlencode(f1.readall)
f1.close
set fso=nothing
fileform filename,content,filename1
end function
function fileform(filename,content,filename1)
%>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<form name="form1" method="post" action="<%= url%>?action=savefile">
<tr>
<td><input name="filename1" type="hidden" value="<%= filename1%>"><input name="filename" type="text" value="<%= filename%>" style="width:100%"></td>
</tr>
<tr>
<td><textarea name="content" wrap="virtual" style="width:100%;height:100%;font:arial,helvetica,sans-serif;" onkeyup="style.height=this.scrollheight;"><%= content%></textarea></td>
</tr>
<tr>
<td><center><input type="submit" name="submit" value="保存" style="width:200"><input type="reset" name="submit2" value="重置" style="width:200"></center></td>
</tr>
</form>
</table>
<%
end function
'文件夹操作函数
function delfolder()
foldername=request.querystring("foldername")
set fso=server.createobject("scripting.filesystemobject")
tempurl=url&"?foldername="&fso.getparentfoldername(foldername)
fso.deletefolder(foldername)
set fso=nothing
response.redirect(tempurl)
end function
function savefolder()
foldername=request.form("foldername")
foldername1=request.form("foldername1")
set fso=server.createobject("scripting.filesystemobject")
if foldername1="" then
set f=fso.createfolder(foldername)
else
fso.movefolder foldername1,foldername
end if
tempurl=url&"?foldername="&f
set fso=nothing
response.redirect(tempurl)
end function
function newfolder()
foldername=request.querystring("foldername")&"\"
formfolder foldername,foldername1
end function
function editfolder()
foldername=request.querystring("foldername")
foldername1=request.querystring("foldername")
formfolder foldername,foldername1
end function
function formfolder(foldername,foldername1)
%>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<form name="form1" method="post" action="<%= url%>?action=savefolder">
<tr><td><input name="foldername1" type="hidden" value="<%= foldername1%>"><input name="foldername" type="text" value="<%= foldername%>" style="width:100%"></td></tr>
<tr><td><center><input type="submit" name="submit" value="保存" style="width:200"><input type="reset" name="submit2" value="重置" style="width:200"></center></td></tr>
</form>
</table>
<%
end function
function upload()
%>
<form action="file.asp?action=saveupload" method="post" enctype="multipart/form-data" name="form2">
<div align="center">有待完成
<input type="file" name="file" style="width:80%">
<input type="submit" name="submit" value="提交">
</div>
</form>
<%
end function
function saveupload()
'有待实现
end function
'登陆
function chklogin()
password=request.form("password")
if password="ok" then session("login")="login"
if session("login")<>"login" then login
end function
function login()
%>
<form name="form1" method="post" action="<%= url%>?action=chklogin">
<center>请输入密码:<input name="password" type="password"><input type="submit" name="submit" value="登陆"></center>
</form>
<%
response.end()
end function
function logout()
session.abandon()
response.redirect(url)
end function
%>
<title>http://bbs.asp2004.net 制作:小灰 qq:103895</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
a:link {
font-size: 9pt;
}
a:visited {
font-size: 9pt;
}
a:hover {
font-size: 9pt;
}
a:active {
font-size: 9pt;
}
body {
font-size: 9pt;
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
line-height: 20px;
}
td {
font-size: 9pt;
line-height: 20px;
}
-->
</style>
<%
action=request.querystring("action")
temp=split(request.servervariables("url"),"/")
url=temp(ubound(temp))
chklogin()
select case action
case "logout"
logout()
case "delfile"
delfile()
case "savefile"
savefile()
case "editfile"
fileedit()
case "newfile"
filenew()
case "upload"
upload()
case "saveupload"
saveupload()
case "delfolder"
delfolder()
case "savefolder"
savefolder()
case "editfolder"
editfolder()
case "newfolder"
newfolder()
case else
foldername=request.querystring("foldername")
filename=request.querystring("filename")
path=foldername
if path="" then path=server.mappath("./")
showfolderlist(path)
end select
function showfolderlist(folderspec)
temp=request.servervariables("http_referer")
temp=left(temp,instrrev(temp,"/"))
temp1=len(folderspec)-len(server.mappath("./"))-1
if temp1>0 then
temp1=right(folderspec,cint(temp1))
elseif temp1=-1 then
temp1=""
end if
tempurl=temp+replace(temp1,"\","/")+"/"
set fso = createobject("scripting.filesystemobject")
upfolderspec=fso.getparentfoldername(folderspec&"\")
%>
<a href="<%= url%>?action=newfolder&foldername=<%= folderspec%>">新建文件夹</a> | <a href="<%= url%>?action=newfile&filename=<%= folderspec&"\"%>">新建文件</a> | <a href="<%= url%>?action=upload">上传文件</a> | <a href="<%= url%>?foldername=<%= upfolderspec%>">向上</a> | <a href="file.asp">返回首页</a> | <a href="<%= url%>?action=logout">退出</a>
<table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#6595d6">
<tr bgcolor="#e4e8ef">
<td width="64%"><div align="center">名称</div></td>
<td width="7%"><div align="right">大小</div></td>
<td width="6%"><div align="center">类型</div></td>
<td width="15%"><div align="left">修改时间</div></td>
<td width="8%"><div align="center">操作</div></td>
</tr>
<%
'列出目录
set f = fso.getfolder(folderspec)
set fc = f.subfolders
for each f1 in fc
%>
<tr bgcolor="#e4e8ef" onmouseout=this.bgcolor='#e4e8ef'; onmouseover=this.bgcolor='#ffffff';>
<td><a href="<%= url%>?foldername=<%= folderspec%>\<%= f1.name%>"><%= f1.name%></a></td>
<td><div align="right"><%= f1.size%></div></td>
<td><div align="center">文件夹</div></td>
<td><div align="left"><%= f1.datelastmodified%></div></td>
<td><a href="<%= url%>?action=editfolder&foldername=<%= folderspec&"\"&f1.name%>">修改</a> <a href="<%= url%>?action=delfolder&foldername=<%= folderspec&"\"&f1.name%>" onclick="{if(confirm('删除文件夹<<%= f1.name%>>么?')){return true;}return false;}">删除</a></td>
</tr>
<%
next
'列出文件
set fc = f.files
for each f1 in fc
%>
<tr bgcolor="#e4e8ef" onmouseout=this.bgcolor='#e4e8ef'; onmouseover=this.bgcolor='#ffffff';>
<td><a href="<%= tempurl+f1.name%>" target="_blank"><%= f1.name%></a></td>
<td><div align="right"><%= f1.size%></div></td>
<td><div align="left"></div></td>
<td><div align="left"><%= f1.datelastmodified%></div></td>
<td><a href="<%= url%>?action=editfile&filename=<%= folderspec&"\"&f1.name%>">修改</a> <a href="<%= url%>?action=delfile&filename=<%= folderspec&"\"&f1.name%>" onclick="{if(confirm('删除文件<<%= f1.name%>>么?')){return true;}return false;}">删除</a></td>
</tr>
<%
next
set fso=nothing
%>
</table>
<%
end function
'文件操作
function delfile()
filename=request.querystring("filename")
set fso=server.createobject("scripting.filesystemobject")
fso.deletefile(filename)
tempurl=url&"?foldername="&fso.getparentfoldername(filename)
set fso=nothing
response.redirect(tempurl)
end function
function savefile()
filename=request.form("filename")
content=request.form("content")
filename1=request.form("filename1")
if request.servervariables("path_translated")<>filename then
set fso=server.createobject("scripting.filesystemobject")
set f1=fso.opentextfile(filename,2,true)
f1.write(content)
f1.close
if filename1<>"" and filename1<>filename then fso.deletefile(filename1)
tempurl=url&"?foldername="&fso.getparentfoldername(filename)
set fso=nothing
end if
response.redirect(tempurl)
end function
function filenew()
filename=request.querystring("filename")
content=""
fileform filename,content,filename1
end function
function fileedit()
filename=request.querystring("filename")
filename1=request.querystring("filename")
set fso=server.createobject("scripting.filesystemobject")
set f1=fso.opentextfile(filename,1,true)
content=server.htmlencode(f1.readall)
f1.close
set fso=nothing
fileform filename,content,filename1
end function
function fileform(filename,content,filename1)
%>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<form name="form1" method="post" action="<%= url%>?action=savefile">
<tr>
<td><input name="filename1" type="hidden" value="<%= filename1%>"><input name="filename" type="text" value="<%= filename%>" style="width:100%"></td>
</tr>
<tr>
<td><textarea name="content" wrap="virtual" style="width:100%;height:100%;font:arial,helvetica,sans-serif;" onkeyup="style.height=this.scrollheight;"><%= content%></textarea></td>
</tr>
<tr>
<td><center><input type="submit" name="submit" value="保存" style="width:200"><input type="reset" name="submit2" value="重置" style="width:200"></center></td>
</tr>
</form>
</table>
<%
end function
'文件夹操作函数
function delfolder()
foldername=request.querystring("foldername")
set fso=server.createobject("scripting.filesystemobject")
tempurl=url&"?foldername="&fso.getparentfoldername(foldername)
fso.deletefolder(foldername)
set fso=nothing
response.redirect(tempurl)
end function
function savefolder()
foldername=request.form("foldername")
foldername1=request.form("foldername1")
set fso=server.createobject("scripting.filesystemobject")
if foldername1="" then
set f=fso.createfolder(foldername)
else
fso.movefolder foldername1,foldername
end if
tempurl=url&"?foldername="&f
set fso=nothing
response.redirect(tempurl)
end function
function newfolder()
foldername=request.querystring("foldername")&"\"
formfolder foldername,foldername1
end function
function editfolder()
foldername=request.querystring("foldername")
foldername1=request.querystring("foldername")
formfolder foldername,foldername1
end function
function formfolder(foldername,foldername1)
%>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<form name="form1" method="post" action="<%= url%>?action=savefolder">
<tr><td><input name="foldername1" type="hidden" value="<%= foldername1%>"><input name="foldername" type="text" value="<%= foldername%>" style="width:100%"></td></tr>
<tr><td><center><input type="submit" name="submit" value="保存" style="width:200"><input type="reset" name="submit2" value="重置" style="width:200"></center></td></tr>
</form>
</table>
<%
end function
function upload()
%>
<form action="file.asp?action=saveupload" method="post" enctype="multipart/form-data" name="form2">
<div align="center">有待完成
<input type="file" name="file" style="width:80%">
<input type="submit" name="submit" value="提交">
</div>
</form>
<%
end function
function saveupload()
'有待实现
end function
'登陆
function chklogin()
password=request.form("password")
if password="ok" then session("login")="login"
if session("login")<>"login" then login
end function
function login()
%>
<form name="form1" method="post" action="<%= url%>?action=chklogin">
<center>请输入密码:<input name="password" type="password"><input type="submit" name="submit" value="登陆"></center>
</form>
<%
response.end()
end function
function logout()
session.abandon()
response.redirect(url)
end function
%>
上一篇: 多域名绑定到一个空间访问不同首页的技巧
下一篇: Go语言数据类型