Link to home
Start Free TrialLog in
Avatar of johnharbour
johnharbour

asked on

Batch unzip with values from a txt file

Hi.  I have a group of 800 zip files that I need to unzip, but I only need to unzip 300 that are in a text or csv file.  They need to be unzipped to a new directory.  Is there any solution that can be run from a command line on the PC or a commercial application out there that will unzip files based on the values of the list?

Thanks,
Avatar of cmrobertson
cmrobertson
Flag of United States of America image

you can write a vb or your language of choice prog to use the winzip command line (an add-on) then pass in your parameters
Avatar of vertsyeux
vertsyeux

..or, you could unzip them all to one folder, use the list you have with the pkzip command to rezil only the selected files to a new zip file, then unzip that file to your final target directory
Avatar of johnharbour

ASKER

trying to do it in one step due to time constraints.
ASKER CERTIFIED SOLUTION
Avatar of cmrobertson
cmrobertson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BillDL
>>> "but I only need to unzip 300 that are in a text or csv file" <<<

Are saying is that you only want to unzip those archives that CONTAIN *.txt or *.csv files
OR
that you have a list of file names for the archives you want to unzip?

Sorry if I didn't understand that, but it seemed ambiguous to me.

Working from a list of *.zip file names would present no great problem if you use the free 7-Zip which is pretty easy to run from the comand line.

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

Install it and you will find the program folder at "C:\Program Files\7-Zip" if you choose the defaults.  The "windows" executable is "7zFM.exe", but you would be calling the command line program executable "7z.exe".  Print off the "Command Line" usage pages as a reference so you an fine-tune a batch file to suit your requirements if I don't get it quite right. Better still, get a text file with the command line switches:

7z -h > 7-Zip_Usage.txt
or
7z -? > 7-Zip_Usage.txt

Note that it uses a dash or double dash to preceded command line switches rather than a /  eg. /? for help.

Basic usage for unzipping:

7z.exe e --switch1 --switch2 filename.zip --oc:\extract_dir

I would say that what you want to do is create a new "output directory" named after each of the *.zip files so you can keep track.

The following command should read through a file list one line at a time and, for each line, use whatever text is on that line for the command to execute.  Processing finishes after the last line of the list file is read.  In this case the *.zip files and the list file are assumed to be in the same directory as the list file:

for /f "tokens=* delims=" %a in (list.txt) do 7z.exe e %a --oc:\extract_dir\%~na

taking that a bit at a time.  The %a variable will hold whatever is on each line of the text file as it is being read, and so should contain the name of the *.zip file to extract.  The %a is also being used to name the output directory by modifying it to exclude the extension.

If used in a batch file, you double up the % symbols for each occurrence of %a.

Try out the attached batch file (after renaming to Unzip.cmd) in a test folder containing about 3 of your *.zip files copied in there along with a list file containing either just the file names of the zip files, or the fully qualified paths to their folder (ie. the current folder).

Let us know what happens and we can fine tune it from there if needed.

Unzip.txt
If any of your *.zip files contain folders, ie. if unzipped normally with WinZip or the Windows unzipping function the folder hierarchy is recreated, then you should substitute the e for x like this:

start /b /wait %ZipProg% x "%%a" -o"%CD%\%%~na"

This is especially true if the separate folders packed into the zip files contain files with the same filenames.  With the e command, it recreates the folders empty and unpacks all their contents into the root of the new folder created with the name of the zip file, whereas the x command extracts the files to those separate sub-folders of the new folder created.

It would prompt you before overwriting files of the same name in the destination folder, but you would be there all day saying No, and then have to do it all again to ensure the integrity of the contents.

Note that the start /b /wait is there to pause processing between each zip file and stop it from spawning a new command window for each step.

The program is quite picky about what can or must be double-quoted for it to recognise and work with file/folder names or paths with spaces.  I think my command should be OK for your needs.
Just one more thing that is useful to know.  You can copy out 7z.exe and 7z.dll from the program folder to which it installed and use it in another folder if you wanted to uninstall 7-Zip bust still use the command line part of it.
for /f "tokens=*" %A in (ziplist.txt) do 7z.exe e -o.\%A

This will extract all zip files listed in ZIPLIST.TXT to a folder with a name corresponding to the file name.

You can download 7zip from www dot 7zip dot org.
Glad to see another expert agrees with my suggested 7-Zip usage, and the command used does just what the batch file I attached earlier does.
I would have recommended the same - AFAIK 7zip is available as dll to use and control directly from your app.