Solved

Batch unzip with values from a txt file

Posted on 2009-04-01
10
1,634 Views
Last Modified: 2013-11-10
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,
0
Comment
Question by:johnharbour
10 Comments
 
LVL 7

Expert Comment

by:cmrobertson
Comment Utility
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
0
 
LVL 6

Expert Comment

by:vertsyeux
Comment Utility
..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
0
 

Author Comment

by:johnharbour
Comment Utility
trying to do it in one step due to time constraints.
0
 
LVL 7

Accepted Solution

by:
cmrobertson earned 500 total points
Comment Utility
you can use winzip command line right from the command line - it is free to registered users of winzip, I will drop a portion of its documented features in the code window
Using a command window

You can use the add-on in a command window in any of these ways: 
 

You can type the full name of the WinZip folder followed by the name of the command, which is either "WZZIP" (for zipping) or "WZUNZIP" (for unzipping), followed by the parameters necessary for the operation. For example, if you installed WinZip into the C:\Program Files\WinZip folder and you want to list the contents of MyFile.zip, you might type: 

    "c:\program files\winzip\wzzip" -v MyFile.zip

(Note that you must use quotes around the command name if the name of the folder includes spaces, as shown above. Otherwise, the quotes are not necessary.) 
 

You can copy WZZIP.EXE and WZUNZIP.EXE to a folder specified in your PATH environment variable. Then you can run the commands by simply typing "WZZIP" or "WZUNZIP" and the desired parameters, without the name of the WinZip folder: 

    wzzip -v filename.zip

You can add the WinZip folder to your PATH environment variable, for example: 

    set path=c:\windows;"c:\program files\winzip";...

If you have done this, you can run the commands at the system prompt by simply typing "WZZIP" or "WZUNZIP" followed by the desired parameters, without the name of the WinZip folder:
 
 
 

    wzzip -v filename.zip

(For information on the PATH environment variable, please refer to your Windows documentation. If you use this method regularly, you will probably want to edit AUTOEXEC.BAT so that the WinZip folder is permanently added to the PATH environment variable.) 
 

Using the Run dialog

You can use WZZIP.EXE and WZUNZIP.EXE from the Run dialog by simply typing the command name and any desired parameters. It is not necessary to include the WinZip folder, but you may need to specify the folder for the files you wish to work with. For example: 
 

    wzzip -yp -v c:\temp\filename.zip

We recommend using the -yp option to keep the MS-DOS command prompt window from closing automatically; otherwise, you will not have much time to look at the results of your command. 
 

Using batch files

You can use WZZIP.EXE and WZUNZIP.EXE in MS-DOS batch files using the same rules as shown above for command windows. 
 

The procedure will be similar for script processors other than MS-DOS batch files (application program macros, for example). However, check your documentation for information on how paths should be specified. 
 

Using return codes (errorlevels)

WZZIP and WZUNZIP will in most cases return a nonzero errorlevel in the event of a serious error; otherwise, an errorlevel of 0 is returned. You can use these return codes in batch files and other automated processing. For example: 
 

    wzzip filename.zip *.doc

    If ErrorLevel 1 Goto ErrMsg

    Echo No Known Errors

    Goto Exit

    :ErrMsg

    Echo ***SERIOUS ERROR DETECTED***

    :Exit

Open in new window

0
 
LVL 38

Expert Comment

by:Insignificant Volunteer
Comment Utility
>>> "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
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 38

Expert Comment

by:Insignificant Volunteer
Comment Utility
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.
0
 
LVL 38

Expert Comment

by:Insignificant Volunteer
Comment Utility
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.
0
 
LVL 6

Expert Comment

by:Justin_W_Chandler
Comment Utility
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.
0
 
LVL 38

Expert Comment

by:Insignificant Volunteer
Comment Utility
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.
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
I would have recommended the same - AFAIK 7zip is available as dll to use and control directly from your app.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now