Extract multiple files into multiple folders using 7zip

I need to automate unzipping multiple .zip files to different folders using 7 zip.  For example, I have 4 .zip files in one folder and need to unzip them into their own folders, not in to one.  Ideally, I would like to put the command or script into an SSIS package.
Thank you.
newtoperlpgmAsked:
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.

newtoperlpgmAuthor Commented:
The first thing I am realizing is that I need to create a folder for each .zip file if i want to unzip the file into each separate folder.  Then I can move the zip files to the directory and do the simple extract.
0
Bill PrewIT / Software Engineering ConsultantCommented:
Should the dest folder names be the name of the zip file.


»bp
0
Shaun VermaakTechnical SpecialistCommented:
Just mentioning...
Remember that you can build folder structure inside ZIP which can be maintained when extracting
0
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Bill PrewIT / Software Engineering ConsultantCommented:
Here is a BAT script that should do what you want, adjust the SET lines near the top and give it a try...

@echo off
setlocal

rem Define folder to process and location of 7-zip exe file
set BaseDir=c:\base
set ZipExe=c:\7-zip\7z.exe

rem If base directory not found quit
if not exist "%BaseDir%\" (
  echo.*ERROR* - Base directory not found [%BaseDir%].
  exit /b
)

rem If zip program not found quit
if not exist "%ZipExe%" (
  echo.*ERROR* - Zip program not found [%ZipExe%].
  exit /b
)

REM Process all zip files in folder
for %%A in ("%BaseDir%\*.zip") do (
  REM Extract desired file to destination folder
  if not exist "%BaseDir%\%%~nA" md "%BaseDir%\%%~nA"
  "%ZipExe%" e "%%~A" -o"%BaseDir%\%%~nA"
)

Open in new window


»bp
0
newtoperlpgmAuthor Commented:
Shaun,
I receive the files from an external source and then manually move them and extract them using 7zip.  Can you give me an example of what you meant ...
"Just mentioning...
Remember that you can build folder structure inside ZIP which can be maintained when extracting"
Thank you.
0
newtoperlpgmAuthor Commented:
Bill Prew, thank you I will give this a try.
0
newtoperlpgmAuthor Commented:
my base directory needs to be set dynamically because within my first folder I have subfolders with .zip files



rem Define folder to process and location of 7-zip exe file
set BaseDir=c:\base
0
Bill PrewIT / Software Engineering ConsultantCommented:
That's fine, it's just a variable, can certainly be set and changed at runtime.  So help me understand the logic needed so I can work something up.  Where are the folders / files involved, how do we find them or iterate through them, etc?


»bp
0
newtoperlpgmAuthor Commented:
There is a directory for each day.  For example,

Main\2018\01_Jan\20180118\FileFolder1\Zipfile1.zip
Main\2018\01_Jan\20180118\FileFolder1\Zipfile2.zip
Main\2018\01_Jan\20180118\FileFolder1\Zipfile3.zip
Main\2018\01_Jan\20180118\FileFolder1\Zipfile4.zip

The zip files for each of the four folders need to be extracted into a folder named by file ( for example, I need a folder named Zipfile1 for Zipfile1.zip, a folder named Zipfile2 for Zipfile2.zip, etc...

Thanks so much, this will help tremendously.
0
Bill PrewIT / Software Engineering ConsultantCommented:
There is a directory for each day.  For example,

Main\2018\01_Jan\20180118\FileFolder1\Zipfile1.zip
Main\2018\01_Jan\20180118\FileFolder1\Zipfile2.zip
Main\2018\01_Jan\20180118\FileFolder1\Zipfile3.zip
Main\2018\01_Jan\20180118\FileFolder1\Zipfile4.zip

The zip files for each of the four folders need to be extracted into a folder named by file ( for example, I need a folder named Zipfile1 for Zipfile1.zip, a folder named Zipfile2 for Zipfile2.zip, etc...

Confused, all I see in this comment is a single folder, did you mean something different?  And where should the contents of the ZIP file be placed, in the same folder as the ZIP, or someplace else?


»bp
0
newtoperlpgmAuthor Commented:
Every day the folder will be different, to reflect the date.  

Main\YYYY\MM_MonthName\FolderABCD\Zipfile1.zip
Main\YYYY\MM_MonthName\FolderABCD\Zipfile2.zip
Main\YYYY\MM_MonthName\FolderABCD\Zipfile3.zip
Main\YYYY\MM_MonthName\FolderABCD\Zipfile4.zip

They will be extracted into a folder that should be named same as the zipfile name.  For example, file1 is named NewDocs.zip, then a folder shoudl be created named NewDocs and the zip files should be extracted into the NewDocs folder, etc...
Thank you.
0
Bill PrewIT / Software Engineering ConsultantCommented:
Okay, give this a try, it will find ZIP files in all lower level folders, and extract the ZIP files to a new folder with the ZIP files name, located in the same folder as the ZIP file.

@echo off
setlocal

rem Define folder to process and location of 7-zip exe file
set BaseDir=c:\base
set ZipExe=c:\7-zip\7z.exe

rem If base directory not found quit
if not exist "%BaseDir%\" (
  echo.*ERROR* - Base directory not found [%BaseDir%].
  exit /b
)

rem If zip program not found quit
if not exist "%ZipExe%" (
  echo.*ERROR* - Zip program not found [%ZipExe%].
  exit /b
)

REM Process all zip files in folder
for /r "%BaseDir%" %%A in ("*.zip") do (
  REM Extract desired file to destination folder
  if not exist "%%~dpnA" md "%%~dpnA"
  "%ZipExe%" e "%%~A" -o"%%~dpnA"
)

Open in new window


»bp
0

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
newtoperlpgmAuthor Commented:
I tried to run it and I did not see the files get zipped into the expected folder.
0
newtoperlpgmAuthor Commented:
is there a way to test the script and watch what is occurring like from the command line?
Thank you.
0
newtoperlpgmAuthor Commented:
I changed my base directory and was able to run it successfully.  Now I just have to dynamically change my date on my base directory and I should be good to go.  thank you for your help.
0
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.