【问题标题】:How to create target directory structure for copy files in classic ASP?如何在经典 ASP 中为复制文件创建目标目录结构?
【发布时间】:2009-09-07 15:03:25
【问题描述】:

我想复制一个文件到目标目录。 使用文件系统对象的 copyFile 命令很简单。 但我需要一些增强功能,例如,

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

你能帮我实现吗?

如果有其他方法可以让我知道。

谢谢。

解决方案:

'Create folder if it doesn't exist
If not oFSO.FolderExists(sDestinationFolder) then
    oFSO.CreateFolder(sDestinationFolder)
End If

【问题讨论】:

  • @Vikas:在您的问题中看到解决方案令人困惑,只需评论您已将答案中的集合删除就足够了。

标签: asp-classic directory copying


【解决方案1】:

这是我这份工作的基本职能:-

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】:

类似这样的:

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

【讨论】:

  • 好吧,它只适用于只有一个文件夹要创建的情况。我需要这个来处理多个创建文件夹。例如/export/new/new1/new2 等,并认为只有导出文件夹存在。
  • 我的 VB 有点生疏了,但是这(查看我的最新编辑)不起作用吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-09
  • 2010-10-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 2021-12-04
相关资源
最近更新 更多