在ASP中自動創建多級文件夾的函數(使用FSO)

FSO中有個方法是CreateFolder,但是這個方法只能在其上一級文件夾存在的情況下創建新的文件夾,所以我就寫了一個自動創建多級文件夾的函數,在生成靜態頁面等方面使用非常方便.

函數:
  
' --------------------------------
' 自動創建指定的多級文件夾
' strPath爲絕對路徑
' 引用請保留版權
' by im286_Anjer
' 2005-4-3
Function AutoCreateFolder(strPath) ' As Boolean
        On Error Resume Next

        Dim astrPath, ulngPath, i, strTmpPath
        Dim objFSO

        If InStr(strPath, "\") <=0 Or InStr(strPath, ":") <= 0 Then
                AutoCreateFolder = False
                Exit Function
        End If
        Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
        If objFSO.FolderExists(strPath) Then
                AutoCreateFolder = True
                Exit Function
        End If
        astrPath = Split(strPath, "\")
        ulngPath = UBound(astrPath)
        strTmpPath = ""
        For i = 0 To ulngPath
                strTmpPath = strTmpPath & astrPath(i) & "\"
                If Not objFSO.FolderExists(strTmpPath) Then
                        ' 創建
                        objFSO.CreateFolder(strTmpPath)
                End If
        Next
        Set objFSO = Nothing
        If Err = 0 Then
                AutoCreateFolder = True
        Else
                AutoCreateFolder = False
        End If
End Function 
 


調用方法:

MyPath = "C:\a\b\c\"
If AutoCreateFolder(MyPath) Then
        Response.Write "創建文件夾成功"
Else
        Response.Write "創建文件夾失敗"
End If 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章