如何遍历 zip 文件中的子文件夹并解压缩具有特定扩展名的文件?
How to traverse subfolders in a zip file and unzip files with specific extension?
基本上我正在尝试将一些特定文件解压缩到 zip 文件中(其中有很多垃圾子文件夹)。
问题是只有最后一个子文件夹包含我想要的文件。其他子文件夹将不包含除另一个子文件夹之外的任何文件。
这是我目前使用的代码:
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
我是否可以遍历子文件夹并且只解压具有特定扩展名的文件?
您可以使用递归过程来完成此操作,该过程调用自身以获取文件夹项并提取具有特定扩展名的文件项:
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"
基本上我正在尝试将一些特定文件解压缩到 zip 文件中(其中有很多垃圾子文件夹)。
问题是只有最后一个子文件夹包含我想要的文件。其他子文件夹将不包含除另一个子文件夹之外的任何文件。
这是我目前使用的代码:
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
我是否可以遍历子文件夹并且只解压具有特定扩展名的文件?
您可以使用递归过程来完成此操作,该过程调用自身以获取文件夹项并提取具有特定扩展名的文件项:
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"