欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp中创建多级目录的两段代码

程序员文章站 2023-11-24 20:17:34
复制代码 代码如下:        '===========================...
复制代码 代码如下:

        '==============================
        '创建多级目录,可以创建不存在的根目录
        '参数:要创建的目录名称,可以是多级
        '创建目录的根目录从当前目录开始
        '''调用举例
        ''call createmultifolder("/upload/jumbot/myphoto/")
        '==============================

        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

上面的是循环
下面的是正则
复制代码 代码如下:

        '==============================
        ''创建文件目录(多层)
        ''使用正则
        '==============================

        function create_n_folder(save, byval path)
                dim fso
                set fso = server.createobject("scripting.filesystemobject")
                if not fso.folderexists(path) then
                        dim regex
                        set regex = new regexp
                        regex.pattern = "^(.*)\\([^\\]*)$"
                        regex.global = false
                        regex.ignorecase = true
                        save = save & regex.replace(path, "$2") & "|"
                        path = regex.replace(path, "$1")
                        if create_n_folder(save, path) then create_n_folder = true
                        set regex = nothing
                else
                        if save = "|" then
                                create_n_folder = true
                        else
                                dim temp
                                temp = mid(save, 2, len(save) - 2)
                                if instrrev(temp, "|") = 0 then
                                        save = "|"
                                        path = path & "\" & temp
                                else
                                        dim folder
                                        folder = mid(temp, instrrev(temp, "|") + 1)
                                        save = "|" & mid(temp, 1, instrrev(temp, "|") - 1) & "|"
                                        path = path & "\" & folder
                                end if
                                fso.createfolder path
                                if create_n_folder(save, path) then create_n_folder = true
                        end if
                end if
                set fso = nothing
        end function