一个可以自动创建多级目录的函数
程序员文章站
2023-11-16 21:58:16
<% '创建多级目录,可以创建不存在的根目录 '参数:要创建的目录名称,可以是多级 '返回逻辑值,true成功,false失败 '创建目录的根目录从当前目录开始 '-...
<%
'创建多级目录,可以创建不存在的根目录
'参数:要创建的目录名称,可以是多级
'返回逻辑值,true成功,false失败
'创建目录的根目录从当前目录开始
'---------------------------------------------------
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
createfolder = replace(createfolder, "", "/")
if left(createfolder, 1) = "/" then
createfolder = right(createfolder, len(createfolder) -1)
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
%>
'使用方法:
<% response.write createmultifolder("/upload/2006/11/11/") &"<br>" %>
'函数的返回值为true(成功)或false(失败,可能是主机不支持fso功能)
'创建多级目录,可以创建不存在的根目录
'参数:要创建的目录名称,可以是多级
'返回逻辑值,true成功,false失败
'创建目录的根目录从当前目录开始
'---------------------------------------------------
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
createfolder = replace(createfolder, "", "/")
if left(createfolder, 1) = "/" then
createfolder = right(createfolder, len(createfolder) -1)
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
%>
'使用方法:
<% response.write createmultifolder("/upload/2006/11/11/") &"<br>" %>
'函数的返回值为true(成功)或false(失败,可能是主机不支持fso功能)