如何在 vbscript 中解压缩受密码保护的文件? [英] How to unzip a password-protected file in vbscript?

查看:30
本文介绍了如何在 vbscript 中解压缩受密码保护的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 VBScript 的新手.我已经对我想要完成的工作进行了一些广泛的研究,甚至找到了一些示例来说明要做什么,但无法使其正常工作.

I am fairly new to VBScript. I have done some extensive research on what I am trying to accomplish and have even found examples of what to do, but cannot get it to work properly.

在我的完美世界中,我需要解压缩从第三方供应商发送到文件夹的所有压缩文件,将解压缩文件导入不同的文件夹,然后删除压缩文件.

In my perfect world, I need to unzip all zipped files sent to a folder from a third-party vendor, import the unzipped file into a different folder then delete the zipped file.

以下脚本适用于无密码保护的 zip 文件,但供应商发送的所有文件都有密码.正如在另一个帖子中看到的,我已经注释掉了以下几行应该插入密码但不要....(pwd+myZipfile)"和...(pwd+extractTo)".

The script below works properly for non-password protected zip files, but all files sent from the vendor have passwords. As seen in another post, the following lines which I have commented out should insert the password but do not. "...(pwd+myZipfile)" and "...(pwd+extractTo)".

在此先感谢您的帮助.请提出任何代码改进或其他方法来实现这一点.

Thank you in advanced for your help. Please suggest any code improvements or other methods to make this happen.

pathToZipFile = "P:\ZipFiles"  
extractTo = "P:\UnZip"    
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(pathToZipFile)
Set fc = f.Files
Dim myZipFile 
Dim intOptions, objShell, objSource, objTarget
Dim pwd
pwd = "password" 

For Each f1 in fc
On Error Resume Next

    myZipFile = f1

    '    Create the required Shell objects
    Set objShell = CreateObject( "Shell.Application" )

    '    Create a reference to the files and folders
    'Set objSource = objShell.NameSpace(pwd+myZipFile).Items( ) 
    Set objSource = objShell.NameSpace(myZipFile).Items( )

    '     Create a reference to the target folder
    Set objTarget = objShell.NameSpace(pwd+extractTo)
    Set objTarget = objShell.NameSpace(extractTo)
    intOptions = 256

    '     UnZIP the file
    objTarget.CopyHere objSource, intOptions

    '     Release the objects
    Set objSource = Nothing
    Set objTarget = Nothing
    Set objShell  = Nothing

    'Delete File from "P:\ZipFiles" after unzipping
    fso.DeleteFile f1, True

Next

推荐答案

如果你仔细看看 answerpwd+... 的来源,您会注意到 pwd 确实包含密码,但是路径.该变量可能以 Unix 命令 pwd 命名,代表打印工作目录".

If you take a closer look at the answer from which the pwd+... came, you'll notice that pwd does not contain a password, but a path. The variable was probably named after the Unix command pwd, which stands for "print working directory".

据我所知,Shell.Application 对象不支持解压受密码保护的 Zip 文件.您引用的问题的另一个答案建议使用 DotNetZip 库.或者你可以使用 7-zip:

As far as I know the Shell.Application object does not support unpacking password-protected Zip files. Another answer to the question you referenced suggests the DotNetZip library. Or you could use 7-zip:

Function qq(str)
  qq = Chr(34) & str & Chr(34)
End Function

zipfile  = "..."
password = "..."

Set sh = CreateObject("WScript.Shell")
sh.Run "7za.exe x " & qq(zipfile) & " -p" & qq(password), 0, True

这篇关于如何在 vbscript 中解压缩受密码保护的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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