木子屋 Dnawo's BLOG

使用FSO自动创建多级文件夹

👤 dnawo 📅 2007-11-01 👁 4065 👍 0 💬 0 🔄 草根站长站
FileSystemObject中有个方法创建文件夹的方法CreateFolder,但是这个方法只能在其上一级文件夹存在的情况下创建新的文件夹,所以我就写了一个自动创建多级文件夹的函数,在生成静态页面等方面使用非常方便。函数如下:

'-------------------------------- 
'自动创建指定的多级文件夹 
'strPath为绝对路径 
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 = Server.MapPath("a/b/c") 
If AutoCreateFolder(MyPath) Then 
        Response.Write "创建文件夹成功" 
Else 
        Response.Write "创建文件夹失败" 
End If

评论(0)

暂无评论。

计算题
评论需审核通过后显示
← 上一篇 让数据库连接仅在需要时打开 下一篇 → 初学者来认识OLEDB和ODBC的区别