Rename extracted file based on zip file in Batch
Asked Answered
M

3

5

I have multiple zip files with names such as 001.zip, 002.zip, 003.zip and have the potential to go up to 999.zip. Each zip file has exactly one text file. I would like to extract each zip file using Batch, and then rename the text file it extracted to the file name of the zip.

For example, if I extract 001.zip, I want the text file that gets extracted (all the text files that get extracted have different names) to be name 001.txt.

I at least am extracting all the files right now, but I am too unfamiliar with Batch, and am not sure if there is a simple way to do this?

cd test
echo     Decompressing zip4 data.
7z e *.zip
Messina answered 24/7, 2012 at 15:59 Comment(0)
T
5

Try this

md textfiles
for %%f in (*.7z) do (
7z e "%%f"
move *.txt textfiles\%%~nf.txt
)
xcopy textfiles\*.txt originalfolder
rd textfiles /s /q

Just make sure there are no existing text files in this folder and this should work. It just creates a temporary folder and moves all text files there (only the extracted one will be present at the time but this works as the filename is not known) and renames it to the zip file name.

After all the extractions and renames are done it just moves all the textfiles back to the original folder and deletes the temporary one.

Hope this helps.

Trackman answered 24/7, 2012 at 16:11 Comment(1)
Sorry, I forgot to mention that the text files do not have the same names.Messina
S
7

for %F in (*.zip) do 7z e "%F" -so >"%~nF.txt" - provided there is only 1 file in in zip archive as you said (shown as if executed directly from command line, if used in batch use %%F)

Skintight answered 24/7, 2012 at 16:39 Comment(0)
T
5

Try this

md textfiles
for %%f in (*.7z) do (
7z e "%%f"
move *.txt textfiles\%%~nf.txt
)
xcopy textfiles\*.txt originalfolder
rd textfiles /s /q

Just make sure there are no existing text files in this folder and this should work. It just creates a temporary folder and moves all text files there (only the extracted one will be present at the time but this works as the filename is not known) and renames it to the zip file name.

After all the extractions and renames are done it just moves all the textfiles back to the original folder and deletes the temporary one.

Hope this helps.

Trackman answered 24/7, 2012 at 16:11 Comment(1)
Sorry, I forgot to mention that the text files do not have the same names.Messina
W
0

I had a very similar problem and I adopted the answer by wmz with the %%F twist for batch files applied.

My situation was slightly different in minor details:

  • many Zip and 7-Zip archive files in one directory (i.e. multiple archive types)
  • every archive file contains (among other files) exactly one JPG file
  • every JPG in every archive has a different (randomized) name
  • only the JPG file of every archive should be extracted
  • the extracted files should be renamed to something like "archive-name.jpg"

My solution was a CMD/Batch file with this core functionality:

for %%F in (*.zip, *.7z) do 7z e "%%F" *.jpg -so >"%%~nF.jpg"
What answered 21/1, 2021 at 2:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.