Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a bunch of zip files in a folder. Each zip file has a file in it named exp_1.txt. I would like to unzip each zip file and rename the extracted file from each zip file to what the zip file was named.

The zip files names don't follow any pattern or date structure, but are all different. I have figured out how to get the exp_1.txt file extracted into the same directory as the zip file, but can't figure out the renaming of exp_1.txt to match what the zip file it was extracted from is named.

Once again in fewer words: from TuesdayReport.zip I want to extract the only file exp_1.txt and rename it to TuesdayReport.txt in the same directory as TuesdayReport.zip.

Currently I'm using 7z x to extract the file to the same directory the zip file is located in. I would prefer to be able to run this from a batch file.

share|improve this question
    
On which operating system? –  Daniel Andersson Feb 27 '13 at 20:09
    
Windows 7 64 bit sorry for leaving that out originally. –  Carrie Feb 27 '13 at 20:29
    
I can get it to name a folder the same name and put the file in that folder, but this really doesn't help me as the file is still named exp_1.txt and it just created a directory for it, any other options? –  Carrie Feb 27 '13 at 21:41

3 Answers 3

why not using 7zip command line edition, that you can download from here?

inside this zip file you can find a help file in CHm format that has all switches and commands that can help you during your batch process.

share|improve this answer
    
I can get it to name a folder the same name and put the file in that folder, but this really doesn't help me as the file is still named exp_1.txt and it just created a directory for it, any other options? –  Carrie Feb 27 '13 at 21:07

This will extract the file named exp_1.txt from every zip file in the current directory with the name same as zip file but with txt instead of zip extension:

for %a in (*.zip) do @(7z l "%~a" -ir!exp_1.txt |findstr exp_1.txt) && 7z e "%~a" -ir!exp_1.txt -so >"%~na.txt"

This is to be run directly from command line, if you want to run it from batch you'd need to double %

What it does:

  • for loop enumerates every zip file
  • first 7z invocation together with findstr makes sure extract is run/a file is only produced if the archive contains particular file
  • second 7z invocation actually extracts the file into the standard output (-so switch) and redirects stdout to file named the same as archive (minus extension). The %~n modifier instructs cmd to take only base name (without extension)

Please note that if there are more than one file in the archive with the same name (in different directories) then it may produce unexpected results.

If you put it in batch then I would also suggest to set filename as variable (before the loop) to avoid repetition related errors (you need to enter it 3 times in the command above).

share|improve this answer
forfiles /M *.zip /C "cmd /c C:\PROGRA~1\7-Zip\7z.exe e @file & ren exp_1.txt @fname.txt"
share|improve this answer
3  
Welcome to the Super User!  While this may answer the question, it would be a better answer if you could explain why it does so.  We’re looking for comprehensive, high-quality answers that provide some explanation and context.  Don’t just give a one-line answer; explain why your answer is right, ideally with citations.  Answers that don’t include explanations may be removed. –  G-Man Apr 11 at 17:40
3  
Can you explain what this code does? As it stands, this isn't very useful and may be deleted. –  DragonLord Apr 11 at 18:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.