如何在不创建递归文件夹的情况下提取

时间:2014-07-01 10:07:45

标签: windows-7 vbscript unzip

我无法在Windows 7上使用VBS将递归编码的zip文件提取到平面目标目录中。这是zip提取代码的核心部分:

objTarget.CopyHere objSource,intOptions

从这个值列表中,intOptions的值为4096: http://www.robvanderwoude.com/vbstech_files_zip.php#CopyHereUNZIP

因此,zip中的文件通常被提取到一个或多个子文件夹(而不是所选文件夹),文件将提取到这些子文件夹。我不必担心文件“互相攻击”,因为只有几个唯一命名的文件。

intOptions的其他值似乎正常工作,例如512,如果操作需要创建新目录,则“不确认创建新目录。”

有什么想法吗?

*编辑:下面的完整工作脚本,根据要求,使用Ansgar的答案*

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 个答案:

答案 0 :(得分:1)

您需要区分文件和文件夹。提取文件,并递归到嵌套文件夹,例如像这样:

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"