3

I have to zip all the files in a folder and make it password protected.

I googled and found one solution which uses the inbuilt functionality of Windows. The code goes like below:

folder1 = "F:\WLMS_TEAM\TOUHID\Script"
zipfile = "F:\WLMS_TEAM\TOUHID\MyTmp.zip"

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.OpenTextFile(zipfile, 8, True).Write "PK" & Chr(5) & Chr(6) _
  & String(18, Chr(0))

Set ShellApp = CreateObject("Shell.Application")
Set zip = ShellApp.NameSpace(zipfile)
zip.CopyHere folder1
WScript.Sleep 2000

Can I make it password protected?

Or if you can help with some other code to use WinZip (not any other tool) to achieve the same.

Or It would be ok to have a separate code to password protect a pre-generated zip file.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Touhid K.
  • 351
  • 1
  • 5
  • 23
  • Since you have chosen to do it only with winzip, i can't reply to you, because i know a solution but in winrar ,so i respect your choice and if you change your mind just tell me – Hackoo Jun 22 '15 at 12:04
  • Sorry, :( We have only winzip full version installed on our server. I have to go ahead with WinZip only. Unfortunately the solution provided by Ansgar is not working for me. – Touhid K. Jun 23 '15 at 05:48

2 Answers2

1

You can run WinZip on the command-line like this:

winzip32.exe -a -s"Password" "C:\path\to\your.zip" *.*

Wrapped in VBScript:

Set sh = CreateObject("WScript.Shell")
sh.Run "winzip32.exe -a -s""Password"" ""C:\path\to\your.zip"" *.*", 0, True

I don't think the Shell.Application object allows the creation of password-protected zip files.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • I have winzip64 installed. When I run your code: Set sh = CreateObject("WScript.Shell") sh.Run "C:\Program Files\WinZip\WINZIP64.EXE -a -sPassword F:\WLMS_TEAM\TOUHID\MyPwdTmp.zip *.*", 0, True I get "The system cannot find the specified file" error. Any issues? – Touhid K. Jun 22 '15 at 10:19
  • My code looks for files in the current directory. Check that the current directory is what you think it is. Also check that the directory `F:\WLMS_TEAM\TOUHID` exists. And make sure that the WinZip executable is either in the `%PATH%` or called with its full path. – Ansgar Wiechers Jun 22 '15 at 10:52
  • 1
    @AnsgarWiechers I think -pPassword used for Winrar and you should use -sPassword for Winzip ! see this Doc ==> http://www.memecode.com/docs/winzip.html – Hackoo Jun 22 '15 at 11:54
  • @Hackoo I actually meant to write `-s`. No idea how that turned into `-p` between my brain and my fingers. Thanks for the heads up. – Ansgar Wiechers Jun 22 '15 at 13:26
0

After a long time of searching, trial and error, I got this working like below: I hope somebody who comes out looking for the similar problems might find it useful.

strWinZipDir = "C:\Program Files\WinZip\WINZIP64.exe"
strZipFileToCreate = LocalPath & "FileName.zip"
strFilesToZip  = LocalPath & "*.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")

strWinZip = objFSO.GetFile(strWinZipDir).ShortPath
strCommand = strWinzip & " -min -a -s""Password"" -r """ & strZipFileToCreate & """ " & strFilesToZip

Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec(strCommand)

Do While objExec.Status = 0
 Wscript.Sleep(200)
Loop

Set objShell = Nothing
Set objExec = Nothing
Set objFSO = Nothing
Touhid K.
  • 351
  • 1
  • 5
  • 23