一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

asp删除文件目录及目录所有文件代码

时间:2010-10-08 编辑:简简单单 来源:一聚教程网

response.expires = -1
response.expiresabsolute = now() - 1
response.expires = 0
response.cachecontrol = "no-cache"
%>




<%
dim act,frompath,page
dim folder_name,file_name,fso,realpath,backurl
act=lcase(trim(request.form("act")))&""
path=trim(request.form("path"))&""
page=trim(request.form("page"))&""

if path="" then
 path=trim(request.querystring("path"))&""
end if
if act="" then
 act=lcase(trim(request.querystring("act")))
end if
if page="" then
 page=lcase(trim(request.querystring("page")))
end if

if right(path,1)<>"/" then
 path=path&"/"
end if
if left(path,1)<>"/" then
 path="/"&path
end if
if invalidchar(path,0) then
 call showmsg("错误:指定的路径含有非法字符!","")
 response.end
end if
call checkpath(pathcanmodify,path)
realpath=server.mappath(path)

select case act
 case "copy","move"
  call copymove()
 case "del","rd","md","copycon","renfile","renfolder"
  call commands()
 case "deltree"
  call deltree()
 case else
  call showmsg("参数丢失,请重新正确操作!","back:off")
  response.end
end select

sub copymove()
 checksubmit()
 frompath=trim(request.form("frompath"))
 if invalidchar(frompath,0) then
  call showmsg("错误:指定的路径含有非法字符!","")
  response.end
 end if
 call checkpath(pathcanmodify,frompath)
 session(sessionprefix&"folders")=""
 session(sessionprefix&"files")=""
 for each folder_name in request.form("folders")
  session(sessionprefix&"folders")=session(sessionprefix&"folders")&"||"&folder_name
 next
 if left(session(sessionprefix&"folders"),2)="||" then
  session(sessionprefix&"folders")=right(session(sessionprefix&"folders"),len(session(sessionprefix&"folders"))-2)
 end if
 for each file_name in request.form("files")
  session(sessionprefix&"files")=session(sessionprefix&"files")&"||"&file_name
 next
 if left(session(sessionprefix&"files"),2)="||" then
  session(sessionprefix&"files")=right(session(sessionprefix&"files"),len(session(sessionprefix&"files"))-2)
 end if
 if session(sessionprefix&"folders")="" and session(sessionprefix&"files")="" then
  session(sessionprefix&"act")=""
  session(sessionprefix&"frompath")=""
  call showmsg("错误:没有选中任何文件和文件夹!","")
 else
  session(sessionprefix&"act")=act
  session(sessionprefix&"frompath")=server.mappath(frompath)
  response.redirect "web_explorer.asp?path="&server.urlencode(path)&"&page="&page
 end if
end sub

sub commands()
  frompath=server.urlencode(trim(request("currentpath")))
  set fso=server.createobject("scripting.filesystemobject")
  backurl="web_explorer.asp?path="&frompath&"&page="&page
  if act="del" then
   if fso.fileexists(realpath) then
    fso.deletefile(realpath)
    if fso.fileexists(realpath) then
     set fso=nothing
     call showmsg("错误:删除文件失败!请确保您有相应的权限。","back:off")
     response.end
    end if
    set fso=nothing
    'response.redirect(backurl)
   else
    set fso=nothing
    call showmsg("错误:您要删除的文件 "&getselfname(realpath,"")&" 不存在!","back:off")
    response.end
   end if
  elseif act="rd" then
   if trim(path)="/" then
    call showmsg("错误:无法删除网站根目录!","back:off")
    response.end
   end if
   if fso.folderexists(realpath) then
    fso.deletefolder(realpath)
    if fso.folderexists(realpath) then
     set fso=nothing
     call showmsg("错误:删除文件夹失败!请确保您有相应的权限。","back:off")
     response.end
    end if
    set fso=nothing
    'response.redirect(backurl)
   else
    set fso=nothing
    call showmsg("错误:您要删除的文件夹 "&getselfname(realpath,"")&" 不存在!","back:off")
    response.end
   end if
  elseif act="md" then
   if fso.folderexists(realpath) then
    set fso=nothing
    call showmsg("错误:您要新建的目录 "&getselfname(realpath,"")&" 已经存在!","back:off")
    response.end
   end if
   fso.createfolder(realpath)
   if not fso.folderexists(realpath) then
    set fso=nothing
    call showmsg("错误:新建目录失败!请确保您有相应的权限。","back:off")
    response.end
   end if
   set fso=nothing
   'response.redirect(backurl)
  elseif act="copycon" then
   if fso.fileexists(realpath) then
    set fso=nothing
    call showmsg("错误:您要新建的文件 "&getselfname(realpath,"")&" 已经存在!","back:off")
    response.end
   end if
   fso.createtextfile(realpath)
   if not fso.fileexists(realpath) then
    set fso=nothing
    call showmsg("错误:新建文件失败!请确保您有相应的权限。","back:off")
    response.end
   end if
   set fso=nothing
   'response.redirect(backurl)
  elseif act="renfile" or act="renfolder" then
   dim oldname,newname,addchar
   oldname=trim(request("oldname"))
   newname=trim(request("newname"))
   if act="renfolder" then
    addchar="夹"
   end if
   if oldname="" or newname="" then
    set fso=nothing
    call showmsg("错误:旧文件"&addchar&"名和新文件"&addchar&"名都必须不能为空!","back:off")
    response.end
   elseif invalidchar(oldname,1) then
    set fso=nothing
    call showmsg("错误:旧文件"&addchar&"名含非法字符!","back:off")
    response.end
   elseif invalidchar(newname,1) then
    set fso=nothing
    call showmsg("错误:新文件"&addchar&"名含非法字符!","back:off")
    response.end
   end if
   oldname=server.mappath(path&oldname)
   newname=server.mappath(path&newname)
   if act="renfile" then
    if fso.fileexists(oldname) then
     if not fso.fileexists(newname) then
      fso.getfile(oldname).name=trim(request("newname"))
      if not fso.fileexists(newname) then
       set fso=nothing
       call showmsg("错误:更改文件"&addchar&"名失败!请确保您有相应的权限。","back:off")
       response.end
      end if
      set fso=nothing
      'response.redirect(backurl)
     else
      set fso=nothing
      call showmsg("失败:新文件"&addchar&"名 "&trim(request("newname"))&" 的文件"&addchar&"已经存在!","back:off")
      response.end
     end if
    else
     set fso=nothing
     call showmsg("失败:要更改的文件"&addchar&" "&trim(request("oldname"))&" 不存在!","back:off")
     response.end
    end if
   elseif act="renfolder" then
    if fso.folderexists(oldname) then
     if not fso.folderexists(newname) then
      fso.getfolder(oldname).name=trim(request("newname"))
      if not fso.folderexists(newname) then
       set fso=nothing
       call showmsg("错误:更改文件"&addchar&"名失败!请确保您有相应的权限。","back:off")
       response.end
      end if
      set fso=nothing
      'response.redirect(backurl)
     else
      set fso=nothing
      call showmsg("失败:新文件"&addchar&"名 "&trim(request("newname"))&" 的文件"&addchar&"已经存在!","back:off")
      response.end
     end if
    else
     set fso=nothing
     call showmsg("失败:要更改的文件"&addchar&" "&trim(request("oldname"))&" 不存在!","back:off")
     response.end
    end if
   end if
  end if
end sub

sub deltree()
  checksubmit()
  dim folders,files,foldername,filename,tempname
  frompath=trim(request.form("frompath"))
  if invalidchar(frompath,0) then
   call showmsg("错误:指定的路径含有非法字符!","")
   response.end
  end if
  call checkpath(pathcanmodify,frompath)
  frompath=server.mappath(frompath)
  backurl="web_explorer.asp?path="&server.urlencode(path)&"&page="&page
  
  if lcase(trim(request.form("act")))="deltree" then
   set folders=request.form("folders")
   set files=request.form("files")
   set fso=server.createobject("scripting.filesystemobject")
   if folders.count>0 then
    for each foldername in folders
     tempname=frompath&""&foldername
     if fso.folderexists(tempname) then
      fso.deletefolder tempname,true
     end if
    next
   end if
   if files.count>0 then
    for each filename in files
     tempname=frompath&""&filename
     if fso.fileexists(tempname) then
      fso.deletefile tempname,true
     end if
    next
   end if
   set folders=nothing
   set files=nothing
   set fso=nothing
  end if
  'response.redirect(backurl)
end sub

if trim(backurl)&""<>"" then
 backurl=replace(backurl,"","\")
 backurl=replace(backurl,chr(34),""&chr(34))
end if
%>



热门栏目