代码之家  ›  专栏  ›  技术社区  ›  Vikas

如何为经典ASP中的复制文件创建目标目录结构?

  •  1
  • Vikas  · 技术社区  · 15 年前

    我要将文件复制到目标目录。 使用文件系统对象的copyfile命令很简单。 但我需要一些增强,比如,

    如果目标目录不存在,那么它将创建目标目录,然后复制一个文件。

    你能帮我实现吗?

    如果有其他方法,请告诉我。

    谢谢。

    解决方案:

    'Create folder if it doesn't exist
    If not oFSO.FolderExists(sDestinationFolder) then
        oFSO.CreateFolder(sDestinationFolder)
    End If
    
    2 回复  |  直到 15 年前
        1
  •  2
  •   AnthonyWJones    15 年前

    这是我从事这项工作的基本职能:

    Dim gfso : Set gfso = Server.CreateObject("Scripting.FileSystemObject")
    
    Public Sub CreateFolder(path)
    
      If Len(path) = 0 Then Err.Raise 1001, , "Creating path: " & path & " failed"
    
      If Not gfso.FolderExists(path) Then
        CreateFolder gfso.GetParentFolderName(path)
        gfso.CreateFolder path
      End If
    
    End Sub
    
        2
  •  1
  •   Tchami    15 年前

    像这样:

    Set fs=Server.CreateObject("Scripting.FileSystemObject")
    
    //Create folder if it doesn't exist
    If fs.FolderExists("YOURFOLDERPATH") != true Then
        Set f=fs.CreateFolder("YOURFOLDERPATH")
        Set f=nothing
    End If
    
    //Copy your file
    
    set fs=nothing
    

    W3Schools有很多关于如何使用FileSystemObject的例子[这里][1]。

    编辑:

    Set fs=Server.CreateObject("Scripting.FileSystemObject")
    
    folders = Split("YOURFOLDERPATH", "\")
    currentFolder = ""
    
    //Create folders if they don't exist
    For i = 0 To UBound(folders)
        currentFolder = currentFolder & folders(i)
        If fs.FolderExists(currentFolder) != true Then
            Set f=fs.CreateFolder(currentFolder)
            Set f=nothing       
        End If      
        currentFolder = currentFolder & "\"
    Next
    
    //Copy your file
    
    set fs=nothing