4

Hi, I have many zip files located at g:\toto. These zips contain some files. I would like to extract all zip in a same directory (g:\toto\extracted) then rename various files of the zip.

Example 1 :

www_12567.vp.zip : 3 files : alpha.doc, beta.xls, teta.doc

I would like after extraction, files are renamed with the name of the zip

www_12567.vp.alpha.doc, www_12567.vp.beta.xls, www_12567.vp.teta.doc

Example 2 :

www_12.vp.zip : 3 files : al.doc, bea.xls, tta.doc
www_12.vp.al.doc, www_12.vp.bea.xls, www_12.vp.tta.doc

I found this question, but it talks about .txt and the zip contain one file, so, it doesn't work.

Community
  • 1
  • 1

1 Answers1

4

Without knowing the contents of the archive you can't know which files to rename, because you are putting them into a directory that may already contain other files.

This, however, would be much easier if there was a dedicated directory to put the files temporarily. Here's how you could use it:

@ECHO OFF
SET "srcdir=G:\toto"
SET "tgtdir=G:\toto\extracted"
SET "tmpdir=G:\toto\extracted-tmp"
FOR %%Z IN ("%srcdir%\*.zip") DO (
  unpack "%%Z" with your favourite tool into "%tmpdir%"
  FOR %%I IN ("%tmpdir%\*") DO MOVE "%%I" "%tgtdir%\%%~nZ.%%~nxI"
)

Of course, the temporary directory would need to be empty before running the batch file. You could add DEL "%tmpdir%\*" somewhere before the loop to make sure it is.

One other note is, the above assumes that the archives do not contain subdirectories or, at least, that the files are extracted without subdirectories.

UPDATE

If you are using the 7-Zip archiver to work with .zip files, then this is how your extract command might look:

7z e "%%Z" -o"%tmpdir%"

Disclaimer: I'm not an active user of 7-Zip. This is what I used as a reference to come up with the above command:

Andriy M
  • 76,112
  • 17
  • 94
  • 154