使用 VBScript 从 ZIP 文件中提取文件 [英] Extract files from ZIP file with VBScript

查看:38
本文介绍了使用 VBScript 从 ZIP 文件中提取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从 ZIP 文件中提取文件时,我使用了以下内容.

When extracting files from a ZIP file I was using the following.

Sub Unzip(strFile)
' This routine unzips a file. NOTE: The files are extracted to a folder '
' in the same location using the name of the file minus the extension.  '
' EX. C:\Test.zip will be extracted to C:\Test '
'strFile (String) = Full path and filename of the file to be unzipped. '
Dim arrFile
    arrFile = Split(strFile, ".")
    Set fso = CreateObject("Scripting.FileSystemObject")
    fso.CreateFolder(arrFile(0) & "\ ")
    pathToZipFile= arrFile(0) & ".zip"
    extractTo= arrFile(0) & "\ "
    set objShell = CreateObject("Shell.Application")
    set filesInzip=objShell.NameSpace(pathToZipFile).items
    objShell.NameSpace(extractTo).CopyHere(filesInzip)
    fso.DeleteFile pathToZipFile, True
    Set fso = Nothing
    Set objShell = Nothing
End Sub 'Unzip

这是有效的,但现在我收到文件存在"错误.

This was working, but now I get a "The File Exists" Error.

这是什么原因?有没有其他选择?

What is the reason for this? Are there any alternatives?

推荐答案

以上所有解决方案都是准确的,但不是最终的.

All above solutions are accurate, but they are not definitive.

如果您尝试将压缩文件解压缩到临时文件夹中,则会立即创建一个显示Temporary Folder For YOURFILE.zip"的文件夹(在C:\DocumentsSettings\USERNAME\Local Settings\Temp) 用于包含在您尝试提取的 ZIP 文件中的 EACH FILE.

If you are trying to extract a zipped file into a temporary folder, a folder that displays "Temporary Folder For YOURFILE.zip" will immediately be created (in C:\Documents and Settings\USERNAME\Local Settings\Temp) for EACH FILE contained within your ZIP file, which you are trying to extract.

没错,如果您有 50 个文件,它将在您的临时目录中创建 50 个文件夹.

That's right, if you have 50 files, it will create 50 folders within your temp directory.

但是如果你有 200 个文件,它会在 99 处停止并崩溃说明 - 文件存在.

But if you have 200 files, it will stop at 99 and crash stating - The File Exists.

...

显然,这不会发生在 Windows 7 上,我在上面看到了这些贡献.但无论如何,我们仍然可以有支票.好的,这就是您修复它的方法:

Apparently, this does not occur on Windows 7 with the contributions I view above. But regardless, we can still have checks. Alright, so this is how you fix it:

    '========================
    'Sub: UnzipFiles
    'Language: vbscript
    'Usage: UnzipFiles("C:\dir", "extract.zip")
    'Definition: UnzipFiles([Directory where zip is located & where files will be extracted], [zip file name])
    '========================
    Sub UnzipFiles(folder, file)
        Dim sa, filesInzip, zfile, fso, i : i = 1
        Set sa = CreateObject("Shell.Application")
            Set filesInzip=sa.NameSpace(folder&file).items
        For Each zfile In filesInzip
            If Not fso.FileExists(folder & zfile) Then
                sa.NameSpace(folder).CopyHere(zfile), &H100 
                i = i + 1
            End If
            If i = 99 Then
            zCleanup(file, i)
            i = 1
            End If
        Next
        If i > 1 Then 
            zCleanup(file, i)
        End If
        fso.DeleteFile(folder&file)
    End Sub

    '========================
    'Sub: zCleanup
    'Language: vbscript
    'Usage: zCleanup("filename.zip", 4)
    'Definition: zCleanup([Filename of Zip previously extracted], [Number of files within zip container])
    '========================
    Sub zCleanUp(file, count)   
        'Clean up
        Dim i, fso
        Set fso = CreateObject("Scripting.FileSystemObject")
        For i = 1 To count
           If fso.FolderExists(fso.GetSpecialFolder(2) & "\Temporary Directory " & i & " for " & file) = True Then
           text = fso.DeleteFolder(fso.GetSpecialFolder(2) & "\Temporary Directory " & i & " for " & file, True)
           Else
              Exit For
           End If
        Next
    End Sub

就是这样,将这两个函数复制并粘贴到您的 VBScript 托管程序中,您应该可以在 Windows XP 和 Windows XP 上运行了.视窗 7.

And that's it, copy and paste those two functions into your VBScript hosted program and you should be good to go, on Windows XP & Windows 7.

谢谢!

这篇关于使用 VBScript 从 ZIP 文件中提取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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