acork
acork

Reputation: 23

How to unzip a password-protected file in 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.

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

Upvotes: 2

Views: 5725

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200193

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

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

Upvotes: 3

Related Questions