如何遍历zip文件中的子文件夹并解压缩具有特定扩展名的文件?

时间:2015-05-13 20:02:18

标签: vbscript

基本上我试图解压缩zip文件中的某些特定文件(其中有很多垃圾子文件夹)。

事情只是最后一个子文件夹包含我想要的文件。其他子文件夹不会包含除另一个子文件夹之外的任何文件。

enter image description here

以下是我目前正在使用的代码:

ZipFile="C:\Test.zip" 
ExtractTo="C:\" 
Set fso = CreateObject("Scripting.FileSystemObject") 
If NOT fso.FolderExists(ExtractTo) Then  
    fso.CreateFolder(ExtractTo) 
End If 
set objShell = CreateObject("Shell.Application") 
set FilesInZip= objShell.NameSpace(ZipFile).items
print "There are " & FilesInZip.Count & " files" 
'Output will be 1 because there is only one subfolder there.
objShell.NameSpace(ExtractTo).CopyHere(FilesInZip) 
Set fso = Nothing 
Set objShell = Nothing

无论如何我可以遍历子文件夹并只解压缩具有特定扩展名的文件吗?

1 个答案:

答案 0 :(得分:2)

您可以使用递归过程为文件夹项调用自身,并在文件项具有特定扩展名时提取文件项:

Set fso = CreateObject("Scripting.FileSystemObject")
Set app = CreateObject("Shell.Application")

Sub ExtractByExtension(fldr, ext, dst)
  For Each f In fldr.Items
    If f.Type = "File folder" Then
      ExtractByExtension f.GetFolder, ext, dst
    ElseIf LCase(fso.GetExtensionName(f.Name)) = LCase(ext) Then
      app.NameSpace(dst).CopyHere f.Path
    End If
  Next
End Sub

ExtractByExtension app.NameSpace("C:\path\to\your.zip"), "txt", "C:\output"
相关问题