欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP如何检测某文件夹是否存在,不存在则自动创建

程序员文章站 2022-03-10 14:43:56
直接给大家分享一下萬仟网测试正常可以使用的代码,并且支持多级目录创建 代码一 function createmultifolder(byval cfolder)...

直接给大家分享一下测试正常可以使用的代码,并且支持多级目录创建

代码一

 function createmultifolder(byval cfolder) 
        dim objfso, phcreatefolder, createfolderarray, createfolder 
        dim i, ii, createfoldersub, phcreatefoldersub, blinfo 
        blinfo = false 
        createfolder = cfolder 
        on error resume next 
        set objfso = server.createobject("scripting.filesystemobject") 
        if err then 
            err.clear() 
            exit function 
        end if 
        if right(createfolder, 1) = "/" then 
            createfolder = left(createfolder, len(createfolder) -1) 
        end if 
        createfolderarray = split(createfolder, "/") 
        for i = 0 to ubound(createfolderarray) 
            createfoldersub = "" 
            for ii = 0 to i 
                createfoldersub = createfoldersub & createfolderarray(ii) & "/" 
            next 
            phcreatefoldersub = server.mappath(createfoldersub) 
            if not objfso.folderexists(phcreatefoldersub) then 
                objfso.createfolder(phcreatefoldersub) 
            end if 
        next 
        if err then 
            err.clear() 
        else 
            blinfo = true 
        end if 
        createmultifolder = blinfo 
end function

使用方法:

createmultifolder("/202003/tools/")

代码二、测试ok

'自动创建多极目录
'code by jb51 reterry
function createit(path)
dim fsofo,cinfo,thepath,thepatharray
dim i,ii,binfo
binfo=false
thepath=path
set fsofo=createobject("scripting.filesystemobject")
if err then
err.clear
exit function
end if
thepath=replace(thepath,"\","/")
if left(thepath,1)="/" then
thepath=right(thepath,len(thepath)-1)
end if
if right(thepath,1)="/" then
thepath=left(thepath,len(thepath)-1)
end if
thepatharray=split(thepath,"/")
for i=0 to ubound(thepatharray)
createfoldersub1=createfoldersub1&thepatharray(i)&"/"
createfoldersub=server.mappath(createfoldersub1)
if not fsofo.folderexists(createfoldersub) then
fsofo.createfolder(createfoldersub)
end if
next
if err then
err.clear
else
binfo=true
end if
createit=binfo
end function

测试代码

createit("/202004/tools/")

以上代码如果无法运行,请检查iis运行用户的权限是否有写功能。今天测试的时候默认iis7.5下是无法运行的。

下面的实现代码功能性简单,适合学习

asp如何检测某文件夹是否存在,不存在则自动创建

folder=server.mappath("/imagess") 
set fso = createobject("scripting.filesystemobject") 
if fso.fileexists(server.mappath(filepath)) then 
respnse.write("都有了还建什么建") 
else 
fso.createfolder(folder) 
end if 
set fso = nothing

dim objfso 
set objfso = server.createobject("scripting.filesystemobject") 
if objfso.folderexists(server.mappath(savepath))=false then 
objfso.createfolder(server.mappath(savepath)) 
end if

folder=server.mappath("/imagess") 
set fso = createobject("scripting.filesystemobject") 
if fso.fileexists(server.mappath(filepath)) then 
respnse.write("都有了还建什么建") 
else 
fso.createfolder(folder) 
end if 
set fso = nothing  

都不完善,我想楼主的意思是创建无极深度目录吧,给个我写的: 

'创建新文件夹(允许无级创建)1:35 2005-1-31 

public function createfolder(folderpath) 
dim sobjfso 
dim arrfolder 
dim i 

set sobjfso = server.createobject("scripting.filesystemobject") 
folderpath = replace(folderpath,"\","/") 
arrfolder = split(folderpath,"/") 
on error resume next 

for i = 0 to ubound(arrfolder) 
if i > 0 then arrfolder(i) = arrfolder(i-1) & "/" & arrfolder(i) 
if not sobjfso.folderexists(arrfolder(i)) then 
sobjfso.createfolder(arrfolder(i)) 
end if 
next 
createfolder = true 

if err.number <> 0 then 
createfolder = false 
err.clear 
end if 
end function 

创建文件夹

dim fso,savepath
savepath=server.mappath(".\"&imagefile&"\"&username&"\"&specialname&"")
set fso = server.createobject("scripting.filesystemobject") 
if fso.folderexists(savepath)=false then 
fso.createfolder(savepath) 
end if
set fso=nothing

删除文件夹

dim fso,savepath
savepath=server.mappath(".\"&imagefile&"\"&username&"\"&specialname&"")
set fso = server.createobject("scripting.filesystemobject") 
if fso.folderexists(savepath)=true then 
fso.deletefolder(savepath) 
end if
set fso=nothing

复制文件

dim fso
set fso=server.createobject("scripting.filesystemobject")

sub copyfiles(path,path2)
 set mycopy=fso.getfile(path)
 response.flush()
 mycopy.copy path2
 response.write("<b>installed success !&nbsp;&nbsp;</b>"&path2&"<br>")
 response.flush()
 end sub
call copyfiles(server.mappath("../无标题2.bmp"),"d:\网站项目\photo\aspupload\07_images\")

下面是其他网友的补充 

public function checkandcreatefolder(foldername)
  fldr = server.mappath(foldername)
  set fso = createobject("scripting.filesystemobject")
  if not fso.folderexists(fldr) then
   fso.createfolder(fldr)
  end if
  set fso = nothing
end function

 检查文件夹是否存在,不存在则创建文件夹,该函数无返回值。

例:checkandcreatefolder("asp")

检查当前目录下是否存在asp文件夹,不存在则创建文件夹asp ,缺点是不支持多级目录创建。

 asp关于fso函数,文件与文件夹的相关操作用得到

'//提供文件处理通用接口
class filesystemobject
'/*
' * 功能描述:删除文件
' * 输入参数:filename——文件相对路径
'*/
public function delfile(filename)
 dim getpath
 getpath="/"
 set fso=server.createobject("scripting.filesystemobject")
 getpath=replace(getpath&filename,"//","/")
 if fso.fileexists(server.mappath(getpath))=true then
   fso.deletefile server.mappath(getpath)
 end if
 set fso=nothing
end function

 

'/*
' * 功能描述:判断路径是否存在,如不存在则创建
' * 输入参数:savefilepath——相对路径,如:/uploadfiles/newsfiles
'*/
public function createpath(savefilepath)
 dim declarepath,fileobj,filepath
 declarepath="/"
 
 set fileobj=server.createobject("scripting.filesystemobject") 
 for each filepath in split(savefilepath,"/") 
   declarepath=replace(declarepath&filepath&"/","//","/") 
   if fileobj.folderexists(server.mappath(declarepath))=false then 
     fileobj.createfolder(server.mappath(declarepath))'创建文件夹
   end if
 next 
 set fileobj=nothing
 createpath=declarepath
end function

 

'/*
' * 功能描述:重命名文件夹
' * 输入参数:getpath——文件夹路径
' * 输入参数:oldname——旧的文件夹名称
' * 输入参数:newname——新的文件夹名称
'*/
public function renfolder(getpath,oldname,newname) 
 dim fso
 if oldname="" or newname="" then
   exit function
 else
   if oldname=newname then exit function
 end if
 set fso=server.createobject("scripting.filesystemobject")
 if fso.folderexists(server.mappath(getpath&newname)) then
   response.write"<script language=javascript>alert('目录已经存在!!');this.history.go(-1);</script>"
   response.end()
 end if
 '//旧的文件夹不存在,则创建
 if not fso.folderexists(server.mappath(getpath&oldname)) then
   createpath(getpath&oldname)
 end if
 
 fso.movefolder server.mappath(getpath&oldname),server.mappath(getpath&newname)
 set fso=nothing
 'response.redirect request.servervariables("http_referer")
end function

 

'/*
' * 功能描述:保存当前文件
' * 输入参数:getpath——文件路径
' * 输入参数:getcontent——保存的内容
' * 输入参数:getfile——保存的文件名
'*/
public function saveeditfile(getpath,getcontent,getfile)
 if getcontent="" or getfile="" then exit function
 set fso=server.createobject("scripting.filesystemobject")
 set cf=fso.createtextfile(server.mappath(getpath&getfile),true)
 cf.write getcontent
 cf.close
 set cf=nothing
 set fso=nothing
 'response.redirect request.servervariables("http_referer")
end function

end class

以上就是asp如何检测某文件夹是否存在,不存在则自动创建的详细内容,更多关于asp如何检测某文件夹是否存在的资料请关注其它相关文章!

上一篇: pjblog2的参数第1/2页

下一篇: jude