How to extract without creating recursive folders

时光总嘲笑我的痴心妄想 提交于 2019-12-13 08:02:11

问题


I've not been able to extract a recursively encoded zip file into a flat target directory using VBS on Windows 7. Here's the core part of the zip extraction code:

objTarget.CopyHere objSource, intOptions

Where intOptions is given the value of 4096, from this list of values: http://www.robvanderwoude.com/vbstech_files_zip.php#CopyHereUNZIP

So where the files in the zip are normally extracted to one or more subfolders (instead of the selected folder), the files extract to those subfolders. I don't have to worry about files "clobbering" each other, since there's only a couple of uniquely named files.

Other values for intOptions seem to work correctly, such as 512, which does "not confirm the creation of a new directory if the operation requires one to be created."

Any ideas?

* Edit: full working script below, as requested, using Ansgar's Answer *

Dim strCurrentFolder, ExtractTo, strCurrentZip, src, dst, fso, strFileName, app

strCurrentFolder = Replace(WScript.ScriptFullName,WScript.ScriptName,"") ' Path where script is running
ExtractTo = strCurrentFolder & "temp1"
WScript.Echo ExtractTo
strFileName = "samplefile.zip"



'If the extraction location does not exist create it.
Set fso = CreateObject("Scripting.FileSystemObject")
If NOT fso.FolderExists(ExtractTo) Then
   fso.CreateFolder(ExtractTo)
End If

'Unzip "C:\path\to\your.zip", "C:\output\folder"
Unzip strCurrentZip, ExtractTo

WScript.Quit



Sub Unzip(src, dst)
' Function to extract all files from a compressed "folder"
' (ZIP, CAB, etc.) using the Shell Folders' CopyHere method
Dim obj, app, intOptions
Set app = CreateObject("Shell.Application")


' Loop through zipped "subfolders"
' http://stackoverflow.com/a/24515198/1569434
  For Each obj In app.NameSpace(src).Items
      If obj.IsFolder Then
     'WScript.Echo "Recursing into nested folders..."
     Unzip obj.Path, dst              'recurse into nested folders
    Else
     'WScript.Echo "Extracting files"
     app.NameSpace(dst).CopyHere obj 'extract files, not using intOptions right now
    End If
  Next
End Sub

回答1:


You need to distinguish between files and folders. Extract files, and recurse into the nested folders, e.g. like this:

Set app = CreateObject("Shell.Application")

Sub Unzip(src, dst)
  For Each obj In app.NameSpace(src).Items
    If obj.IsFolder Then
      Unzip obj.Path, dst              'recurse into nested folders
    Else
      app.NameSpace(dst).CopyHere obj  'extract files
    End If
  Next
End Sub

Unzip "C:\path\to\your.zip", "C:\output\folder"


来源:https://stackoverflow.com/questions/24507585/how-to-extract-without-creating-recursive-folders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!