如何在不创建递归文件夹的情况下提取 [英] How to extract without creating recursive folders

查看:37
本文介绍了如何在不创建递归文件夹的情况下提取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

objTarget.CopyHere objSource, intOptions

其中 intOptions 的值为 4096,来自此值列表:http://www.robvanderwoude.com/vbstech_files_zip.php#CopyHereUNZIP

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

因此,通常将 zip 中的文件解压缩到一个或多个子文件夹(而不是所选文件夹)的位置,文件会解压缩到这些子文件夹.我不必担心文件相互破坏",因为只有几个唯一命名的文件.

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.

intOptions 的其他值似乎可以正常工作,例如 512,它如果操作需要创建新目录,则不确认创建新目录."

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."

有什么想法吗?

* 下面的完整工作脚本,根据要求,使用 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

推荐答案

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

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"

这篇关于如何在不创建递归文件夹的情况下提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆