ASP如何檢測(cè)某文件夾是否存在,不存在則自動(dòng)創(chuàng)建
直接給大家分享一下腳本之家測(cè)試正常可以使用的代碼,并且支持多級(jí)目錄創(chuàng)建
代碼一
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/")
代碼二、測(cè)試ok
'自動(dòng)創(chuàng)建多極目錄 '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
測(cè)試代碼
createit("/202004/tools/")
以上代碼如果無(wú)法運(yùn)行,請(qǐng)檢查iis運(yùn)行用戶(hù)的權(quán)限是否有寫(xiě)功能。今天測(cè)試的時(shí)候默認(rèn)iis7.5下是無(wú)法運(yùn)行的。
下面的實(shí)現(xiàn)代碼功能性簡(jiǎn)單,適合學(xué)習(xí)
ASP如何檢測(cè)某文件夾是否存在,不存在則自動(dòng)創(chuàng)建 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
都不完善,我想樓主的意思是創(chuàng)建無(wú)極深度目錄吧,給個(gè)我寫(xiě)的:
'創(chuàng)建新文件夾(允許無(wú)級(jí)創(chuàng)建)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
創(chuàng)建文件夾
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
復(fù)制文件
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("../無(wú)標(biāo)題2.bmp"),"D:\網(wǎng)站項(xiàng)目\photo\aspupload\07_images\")
下面是其他網(wǎng)友的補(bǔ)充
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
檢查文件夾是否存在,不存在則創(chuàng)建文件夾,該函數(shù)無(wú)返回值。
例:CheckAndCreateFolder("ASP")
檢查當(dāng)前目錄下是否存在ASP文件夾,不存在則創(chuàng)建文件夾ASP ,缺點(diǎn)是不支持多級(jí)目錄創(chuàng)建。
asp關(guān)于fso函數(shù),文件與文件夾的相關(guān)操作用得到
'//提供文件處理通用接口 Class FileSystemObject '/* ' * 功能描述:刪除文件 ' * 輸入?yún)?shù):FileName——文件相對(duì)路徑 '*/ Public Function DelFile(FileName) Dim getPath getPath="/" SET Fso=Server.CreateObject("Scripting.FileSystemObject") getPath=Replace(getPath&FileName,"http://","/") if Fso.FileExists(Server.MapPath(getPath))=True then Fso.DeleteFile Server.mappath(getPath) End if Set Fso=Nothing End Function '/* ' * 功能描述:判斷路徑是否存在,如不存在則創(chuàng)建 ' * 輸入?yún)?shù):SaveFilePath——相對(duì)路徑,如:/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&"/","http://","/") if FileObj.FolderExists(Server.MapPath(DeclarePath))=false then FileObj.CreateFolder(Server.MapPath(DeclarePath))'創(chuàng)建文件夾 end if Next Set FileObj=nothing CreatePath=DeclarePath End Function '/* ' * 功能描述:重命名文件夾 ' * 輸入?yún)?shù):GetPath——文件夾路徑 ' * 輸入?yún)?shù):OldName——舊的文件夾名稱(chēng) ' * 輸入?yún)?shù):NewName——新的文件夾名稱(chēng) '*/ 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('目錄已經(jīng)存在!!');this.history.go(-1);</script>" response.end() end if '//舊的文件夾不存在,則創(chuàng)建 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 '/* ' * 功能描述:保存當(dāng)前文件 ' * 輸入?yún)?shù):GetPath——文件路徑 ' * 輸入?yún)?shù):GetContent——保存的內(nèi)容 ' * 輸入?yún)?shù):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如何檢測(cè)某文件夾是否存在,不存在則自動(dòng)創(chuàng)建的詳細(xì)內(nèi)容,更多關(guān)于ASP如何檢測(cè)某文件夾是否存在的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
一些關(guān)于asp 購(gòu)物車(chē)的想法
剛看到吳磊同學(xué)的一些關(guān)于購(gòu)物車(chē)的想法,正巧本人丁學(xué)對(duì)電子商務(wù)這方面比較熟悉,跳出來(lái)獻(xiàn)丑了,希望對(duì)一些同行有些用處。本來(lái)想回復(fù)到下面的,結(jié)果發(fā)現(xiàn)寫(xiě)起來(lái)比較多,干脆寫(xiě)到這里好了,以后自己找起來(lái)也方便,呵呵2008-11-11ASP基礎(chǔ)入門(mén)第九篇(Global.asa文件的使用)
這篇文章主要介紹了Global.asa文件的使用,是一個(gè)可選文件,每個(gè)應(yīng)用程序只能有一個(gè) Global.asa 文件,需要了解的朋友可以參考下2015-10-10ASP同一站點(diǎn)下gb2312和utf-8頁(yè)面?zhèn)鬟f參數(shù)亂碼的終極解決方法
要解決ASP同一站點(diǎn)下gb2312和utf-8頁(yè)面?zhèn)鬟f參數(shù)亂碼問(wèn)題,只需嚴(yán)格做到以下4點(diǎn)即可。2010-12-12Active Server Pages 錯(cuò)誤 ''ASP 0201'' 修復(fù)方法
網(wǎng)上很多其他的解決方法, 但是我試了幾個(gè)小時(shí), 都沒(méi)有解決問(wèn)題, 最后是通過(guò)這個(gè)方法解決的.2010-07-07用javascript解決外部數(shù)據(jù)抓取中的亂碼問(wèn)題
用javascript解決外部數(shù)據(jù)抓取中的亂碼問(wèn)題...2007-04-04ASP與Excel結(jié)合生成數(shù)據(jù)表和Chart圖的代碼
ASP與Excel結(jié)合生成數(shù)據(jù)表和Chart圖的代碼,需要的朋友可以參考下。2009-12-12