ASP如何检测某文件夹是否存在,不存在则自动创建
直接给大家分享一下毛票票测试正常可以使用的代码,并且支持多级目录创建
代码一
FunctionCreateMultiFolder(ByValCFolder)
DimobjFSO,PhCreateFolder,CreateFolderArray,CreateFolder
Dimi,ii,CreateFolderSub,PhCreateFolderSub,BlInfo
BlInfo=False
CreateFolder=CFolder
OnErrorResumeNext
SetobjFSO=Server.CreateObject("Scripting.FileSystemObject")
IfErrThen
Err.Clear()
ExitFunction
EndIf
IfRight(CreateFolder,1)="/"Then
CreateFolder=Left(CreateFolder,Len(CreateFolder)-1)
EndIf
CreateFolderArray=Split(CreateFolder,"/")
Fori=0ToUBound(CreateFolderArray)
CreateFolderSub=""
Forii=0Toi
CreateFolderSub=CreateFolderSub&CreateFolderArray(ii)&"/"
Next
PhCreateFolderSub=Server.MapPath(CreateFolderSub)
IfNotobjFSO.FolderExists(PhCreateFolderSub)Then
objFSO.CreateFolder(PhCreateFolderSub)
EndIf
Next
IfErrThen
Err.Clear()
Else
BlInfo=True
EndIf
CreateMultiFolder=BlInfo
EndFunction
使用方法:
CreateMultiFolder("/202003/tools/")
代码二、测试ok
'自动创建多极目录
'codebyjb51reterry
functioncreateit(path)
dimfsofo,cinfo,thepath,thepatharray
dimi,ii,binfo
binfo=false
thepath=path
setfsofo=createobject("scripting.filesystemobject")
iferrthen
err.clear
exitfunction
endif
thepath=replace(thepath,"\","/")
ifleft(thepath,1)="/"then
thepath=right(thepath,len(thepath)-1)
endif
ifright(thepath,1)="/"then
thepath=left(thepath,len(thepath)-1)
endif
thepatharray=split(thepath,"/")
fori=0toubound(thepatharray)
createfoldersub1=createfoldersub1&thepatharray(i)&"/"
createfoldersub=server.mappath(createfoldersub1)
ifnotfsofo.folderexists(createfoldersub)then
fsofo.createfolder(createfoldersub)
endif
next
iferrthen
err.clear
else
binfo=true
endif
createit=binfo
endfunction
测试代码
createit("/202004/tools/")
以上代码如果无法运行,请检查iis运行用户的权限是否有写功能。今天测试的时候默认iis7.5下是无法运行的。
下面的实现代码功能性简单,适合学习
ASP如何检测某文件夹是否存在,不存在则自动创建
folder=server.mappath("/imagess")
Setfso=CreateObject("Scripting.FileSystemObject")
iffso.fileexists(Server.mappath(filepath))then
respnse.write("都有了还建什么建")
else
fso.createfolder(folder)
endif
Setfso=nothing
DimobjFSO
SetobjFSO=Server.CreateObject("Scripting.FileSystemObject")
IfobjFSO.FolderExists(Server.MapPath(SavePath))=falseThen
objFSO.CreateFolder(Server.MapPath(SavePath))
EndIf
folder=server.mappath("/imagess")
Setfso=CreateObject("Scripting.FileSystemObject")
iffso.fileexists(Server.mappath(filepath))then
respnse.write("都有了还建什么建")
else
fso.createfolder(folder)
endif
Setfso=nothing
都不完善,我想楼主的意思是创建无极深度目录吧,给个我写的:
'创建新文件夹(允许无级创建)1:352005-1-31
PublicFunctionCreateFolder(FolderPath)
DimsObjFSO
DimarrFolder
Dimi
SetsObjFSO=Server.CreateObject("Scripting.FileSystemObject")
FolderPath=Replace(FolderPath,"\","/")
arrFolder=Split(FolderPath,"/")
OnErrorResumeNext
Fori=0ToUBound(arrFolder)
Ifi>0ThenarrFolder(i)=arrFolder(i-1)&"/"&arrFolder(i)
IfNotsObjFSO.FolderExists(arrFolder(i))Then
sObjFSO.CreateFolder(arrFolder(i))
EndIf
Next
CreateFolder=True
IfErr.number<>0Then
CreateFolder=False
Err.Clear
EndIf
EndFunction
创建文件夹
dimfso,SavePath
SavePath=server.MapPath(".\"&imagefile&"\"&username&"\"&specialname&"")
setfso=server.CreateObject("scripting.filesystemobject")
iffso.FolderExists(SavePath)=falsethen
fso.createfolder(SavePath)
endif
setfso=nothing
删除文件夹
dimfso,SavePath
SavePath=server.MapPath(".\"&imagefile&"\"&username&"\"&specialname&"")
setfso=server.CreateObject("scripting.filesystemobject")
iffso.FolderExists(SavePath)=truethen
fso.deletefolder(SavePath)
endif
setfso=nothing
复制文件
dimfso
setfso=server.CreateObject("scripting.filesystemobject")
subcopyfiles(path,path2)
setmycopy=fso.getfile(path)
response.flush()
mycopy.copypath2
response.write("installedsuccess! "&path2&"
")
response.Flush()
endsub
callcopyfiles(Server.MapPath("../无标题2.bmp"),"D:\网站项目\photo\aspupload\07_images\")
下面是其他网友的补充
PublicFunctionCheckAndCreateFolder(FolderName)
fldr=Server.Mappath(FolderName)
Setfso=CreateObject("Scripting.FileSystemObject")
IfNotfso.FolderExists(fldr)Then
fso.CreateFolder(fldr)
EndIf
Setfso=Nothing
EndFunction
检查文件夹是否存在,不存在则创建文件夹,该函数无返回值。
例:CheckAndCreateFolder("ASP")
检查当前目录下是否存在ASP文件夹,不存在则创建文件夹ASP ,缺点是不支持多级目录创建。
asp关于fso函数,文件与文件夹的相关操作用得到
'//提供文件处理通用接口
ClassFileSystemObject
'/*
'*功能描述:删除文件
'*输入参数:FileName——文件相对路径
'*/
PublicFunctionDelFile(FileName)
DimgetPath
getPath="/"
SETFso=Server.CreateObject("Scripting.FileSystemObject")
getPath=Replace(getPath&FileName,"//","/")
ifFso.FileExists(Server.MapPath(getPath))=Truethen
Fso.DeleteFileServer.mappath(getPath)
Endif
SetFso=Nothing
EndFunction
'/*
'*功能描述:判断路径是否存在,如不存在则创建
'*输入参数:SaveFilePath——相对路径,如:/UploadFiles/NewsFiles
'*/
PublicFunctionCreatePath(SaveFilePath)
DimDeclarePath,FileObj,FilePath
DeclarePath="/"
SetFileObj=Server.CreateObject("Scripting.FileSystemObject")
ForEachFilePathinsplit(SaveFilePath,"/")
DeclarePath=Replace(DeclarePath&FilePath&"/","//","/")
ifFileObj.FolderExists(Server.MapPath(DeclarePath))=falsethen
FileObj.CreateFolder(Server.MapPath(DeclarePath))'创建文件夹
endif
Next
SetFileObj=nothing
CreatePath=DeclarePath
EndFunction
'/*
'*功能描述:重命名文件夹
'*输入参数:GetPath——文件夹路径
'*输入参数:OldName——旧的文件夹名称
'*输入参数:NewName——新的文件夹名称
'*/
PublicFunctionRenFolder(GetPath,OldName,NewName)
DimFso
ifOldName=""orNewName=""then
exitFunction
else
ifOldName=NewNamethenexitFunction
endif
SETFso=Server.CreateObject("Scripting.FileSystemObject")
ifFso.FolderExists(Server.MapPath(GetPath&NewName))then
response.write"alert('目录已经存在!!');this.history.go(-1);"
response.end()
endif
'//旧的文件夹不存在,则创建
ifNotFso.FolderExists(Server.MapPath(GetPath&OldName))Then
CreatePath(GetPath&OldName)
Endif
Fso.MoveFolderServer.MapPath(GetPath&OldName),Server.MapPath(GetPath&NewName)
setFso=nothing
'response.redirectrequest.ServerVariables("HTTP_REFERER")
EndFunction
'/*
'*功能描述:保存当前文件
'*输入参数:GetPath——文件路径
'*输入参数:GetContent——保存的内容
'*输入参数:GetFile——保存的文件名
'*/
PublicFunctionSaveEditFile(GetPath,GetContent,GetFile)
ifGetContent=""orGetFile=""thenexitFunction
SETFso=Server.CreateObject("Scripting.FileSystemObject")
setCF=Fso.CreateTextFile(Server.mappath(GetPath&GetFile),true)
CF.writeGetContent
CF.Close
setCF=nothing
setFso=nothing
'response.redirectrequest.ServerVariables("HTTP_REFERER")
EndFunction
EndClass
以上就是ASP如何检测某文件夹是否存在,不存在则自动创建的详细内容,更多关于ASP如何检测某文件夹是否存在的资料请关注毛票票其它相关文章!