ASP如何檢測某資料夾是否存在,不存在則自動建立
阿新 • • 發佈:2020-02-29
直接給大家分享一下我們測試正常可以使用的程式碼,並且支援多級目錄建立
程式碼一
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,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("installed success ! "&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如何檢測某資料夾是否存在的資料請關注我們其它相關文章!