|
本帖最后由 todaynew 于 2012-4-17 21:34 编辑
用递归向下遍历直到不存在子文件夹时进行复制即可。不过这里存在一种情况就是文件夹下的同级文件夹多余一时,需要考虑是否这些子文件夹下的最后一级是否都需要复制。
以下函数未经测试,大体就这么个意思。
Function CopyLastFolder(FolderPath As String, ChildFolderPath As String)
'功能:复制最后一级子文件夹到指定地址
'参数:FolderPath--文件夹路径,ChildFoliderPath--复制子文件夹的目标地址
'引用:Microsoft Scripting Runtime
Dim myFSO As New FileSystemObject
Dim myFolder As Folder
If myFSO.FolderExists(FolderPath) = True Then
Set myFolder = myFSO(FolderPath)
Call Traversal(myFolder, ChildFolderPath)
End If
set myFolder = nothing
set myFSO=nothing
End Function
Function Traversal(myFolder As Folder, ChildFolderPath As String)
'功能:遍历子文件夹
'参数:myFolder--文件夹对象,ChildFoliderPath--复制子文件夹的目标地址
Dim ChildFolder As Folder
Select Case myFolder.SubFolders.Count
Case Is > 1
MsgBox "本级中子文件夹有多于1,终止遍历!"
Exit Function
Case 1
Set ChildFolder = myFolder.SubFolders(0) '不记得是不是从0开始,如果不对的话改为1
'递归调用
Call Traversal(ChildFolder, ChildFolderPath)
Case 0
myFolder.Copy ChildFolderPath
End Select
set ChildFolder=Nothing
End Function |
|