Script to unzip a folder containing password protected zip files

I know almost nothing about scripting, very basic bat and vbs file use in the past so I will try my best to explain this process I am currently doing manually.   I am using a windows 8.1 desktop for this.  OK, here is the scenario:

I have a folder D:\DATA with 50 to 1000 password protected Zip files, zipfile.abc.  Each zipfile.abc contains two files, file.def and file.ghi.  Following are the steps I have been doing manually.
1. From command prompt go to D:\DATA and enter Rename *.abc to *.zip
2. Open each zip file (double click) in explorer, drag copy file.def to D:\Extract and leave file.ghi in the zip file as I don't need it.  Windows prompts for password which I copied to clipboard and hit ctrl-v to enter and click OK. So password is the same for every file.
3. Once all files are extracted, go to D:\Extract and run Rename *.def *.tif

I hope someone can help me automate this process!  Thanks, Rob
LVL 5
RobOwnerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
So, you want to process all the files in the folder?  And each is a zip, but has a different extension?  Are they all the same alternate extension?

How do we identify which file(s) need to be extracted, and which don't?

~bp
Bill PrewIT / Software Engineering ConsultantCommented:
This should get the job done.  Download and install 7zip and then adjust the SET lines in the bat scrips as needed for your folders and files.

http://www.7-zip.org/

@echo off
setlocal EnableDelayedExpansion

REM Specufy folder where zip files exist, and folder to place extracted files
set DataDir=B:\EE\EE28807799\Data
set DestDir=B:\EE\EE28807799\Extract

REM Specify extension of zip files, and extension of files to extract
set ZipExt=abc
set ExtractExt=def

REM Specify password for zip files
set Password=password

REM Specify full path to 7z.exe file
set ZipExe=C:\Program Files\7-Zip\7z.exe

REM Process all zip files in folder
for %%A in ("%DataDir%\*.%ZipExt%") do (
  REM Extract desired file to destination folder
  "%ZipExe%" e "%%~A" -p%Password% -o"%DestDir%" *.%ExtractExt% 
)

Open in new window

~bp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RobOwnerAuthor Commented:
Bill,

At first glance your script looks like it will do what I need and more with the variables, thanks!  In answer to your original questions, need to process all the files in the folder.  Yes, each file is a zip with an alternate extension.  This is fixed at .abc, and is not a variable.

There are two files in each zip with different extensions (my description in step 2 above is .def and .ghi)  and from the looks of your script you allow me to specify which one to extract so that is fine.  I am embarrassed at the typo in the title of this question, but was very hungry and wanted to leave the office to get dinner for the nagging stomach and wife :)

I hope to give this a try in a little while and will let you know.  Thanks for such a quick response!

Best regards, Rob
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

RobOwnerAuthor Commented:
I entered all the variables, saved this as a .bat file and ran.  Unfortunately it did not work, returning no output or errors.  I double checked the 7zip path as well.  Can you take a second look please?
RobOwnerAuthor Commented:
While I couldn't get the script to work I was able to use your lead and run 7zip from the command line which I copied below.  This extracted all the .def files to the d:\extract folder as I needed.  Then just did a rename *.def *.tif as I had before.

D:\DATA>c:\"Program Files"\7-zip\7z.exe e *.abc -ppassword -od:\extract
 *.def

I had to add the quotes around program files.  Not sure what else could be wrong.   While I got this job done for tomorrow, I have to do this process several times a month so it is worth continuing if you would like to take another look and discuss using the script further.  We can simplify things in that the zipext extractext, and password are all constant so no need to set a variable for them.
Bill PrewIT / Software Engineering ConsultantCommented:
I actually ran the script I posted here as a test before posting, and it worked as needed.  Although I do need to add the rename of the output files to TIF.

Can you post up your modified version of the script that you ran please.

And when you run it, there were .ABC files in the folder you specified?  I can add a little checking around that, but that's about the only reason I would think you would not see any messages displayed.

When you run it make sure you test it by running from a DOS window, not just double clicking in Explorer.  That way you will see the output.

Also for a version you can run every month, we could do all the extracts at once as you did, rather than file by file as I did.  Likely a bit faster.  My question would be, will the zip files be new each time you run, or could the same ones exist from a prior run?  And what do you want to do if the extracted files already exist in the destination folder, or will that be empty for each run?

~bp
RobOwnerAuthor Commented:
I can't put the actual script up that I tried because it is confidential, sorry I know that makes it more difficult. But it is yours, run from a command prompt, not elevated. I put quotes around program files,filled in the variables and saved as unzip.bat.

The abc zip files are new each time and destination folder is empty each time.

I will take another look at it today to see if I did something wrong. Thanks.!
Bill PrewIT / Software Engineering ConsultantCommented:
For a test run change the @echo off to @echo on.  Thant will show each statement in the BAT script as it processes.

Also, in the SET statements, do not use double quotes.  Notice I did not even though the path might have spaces.  When the variable is used below it will be surrounded by quotes to take care of this.  If you did have quotes in a SET, like for the 7zip EXE that is likely causing a problem, try without those.

~bp
RobOwnerAuthor Commented:
While I didn't have time to troubleshoot this to the end your script got the job done when I manually filled in the variables at the command prompt.  Thank you for drastically speeding up a process that took an hour to minutes!!!  Perhaps I will perfect this if/when I have to do it again.
Bill PrewIT / Software Engineering ConsultantCommented:
Okay, let me know if I can be of more help when you have more time to work it.

~bp
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.