ASP函數:檢查檔案夾是否存在,不存在則建立檔案夾

來源:互聯網
上載者:User

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.