我想将文件复制到目标目录。 使用File系统对象的copyFile命令很简单。 但我需要一些增强功能,如
如果目标目录不存在,那么它将创建目标目录,然后复制文件。
你能帮我实现吗?
如果有其他方法可以,请告诉我。
感谢。
解决方案:
'Create folder if it doesn't exist
If not oFSO.FolderExists(sDestinationFolder) then
oFSO.CreateFolder(sDestinationFolder)
End If
答案 0 :(得分:2)
这是我这项工作的基本功能: -
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
答案 1 :(得分:1)
这样的事情:
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