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

时间:2021-04-19 23:58:50

Basically I'm trying to unzip some specific files in a zip file (there are lots of junk subfolders in it).

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

The thing is only the last subfolder contains files I want. Other subfolders won't contain any files except another subfolder.

东西只是最后一个子文件夹包含我想要的文件。其他子文件夹不包含除另一个子文件夹以外的任何文件。

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

Here is the code I'm currently using:

这是我目前使用的代码:

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

Is there anyway I can traverse subfolder and only unzip files with a specific extension?

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

1 个解决方案

#1


You can do that with a recursive procedure that calls itself for folder items and extracts file items if they have a specific extension:

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

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"

#1


You can do that with a recursive procedure that calls itself for folder items and extracts file items if they have a specific extension:

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

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"