• Status: Solved
  • Priority: High
  • Security: Public
  • Views: 486
  • Last Modified:

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.
0
newtoperlpgm
Asked:
newtoperlpgm
  • 9
  • 5
1 Solution
 
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 PrewCommented:
Should the dest folder names be the name of the zip file.


»bp
0
 
Shaun VermaakTechnical Specialist/DeveloperCommented:
Just mentioning...
Remember that you can build folder structure inside ZIP which can be maintained when extracting
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Bill PrewCommented:
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 PrewCommented:
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 PrewCommented:
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 PrewCommented:
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 9
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now