ASP如何检测某文件夹是否存在,不存在则自动创建(2)
都不完善,我想楼主的意思是创建无极深度目录吧,给个我写的:
'创建新文件夹(允许无级创建)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 ! </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
内容版权声明:除非注明,否则皆为本站原创文章。