将每个文件夹中的第一个文件复制到windows(嵌套)中的新位置



我正在尝试以下.vbs脚本将第一个文件从每个文件夹复制到windows中的新位置,但我不确定如何复制嵌套2或3个子文件夹深度的文件。有人能帮忙吗?

Const rootFolder = "c:Rootfolder"
Const targetFolder = "c:TargetFolder"
Set fso = CreateObject("Scripting.FileSystemObject")
For Each objFolder In fso.GetFolder(rootFolder).SubFolders
i = 0
For Each sFile In objFolder.Files
If i = 0 then
fso.GetFile(sFile).Copy targetFolder & "" & fso.GetFileName(sFile),True
i = 1
End if
Next
Next

您可以定义一个函数并在递归地循环文件夹的子文件夹时调用该函数

Const rootFolder = "c:Rootfolder"
Const targetFolder = "c:TargetFolder"
Set fso = CreateObject("Scripting.FileSystemObject")
dim folder
set folder = fso.GetFolder(rootFolder)
'call function from rootfolder
moveFirstFile folder
function moveFirstFile(folder)
dim subFolder
For Each subFolder In folder.SubFolders
i = 0
For Each sFile In subFolder.Files
If i = 0 then
fso.GetFile(sFile).Copy targetFolder & "" & fso.GetFileName(sFile),True
i = 1
End if
Next
'move one level deeper
moveFirstFile subFolder
Next
end function

相关内容

  • 没有找到相关文章

最新更新