Rename file when extracting using 7 zip command line
Asked Answered
A

2

5

I'm using the following command to unzip a file:

"C:\Program Files\7-Zip\7z.exe" x "\\server\folder\backup.gz" -aoa "-o\\server\folder\"

I've been on the 7-zip help section and the examples for using -aoa aren't very clear. Using Google I can see solutions on a website called sevenzip, but it's not available. I also thought I could use the rn command but I wasn't sure where to place it.

Aryan answered 6/11, 2018 at 15:39 Comment(3)
No problems here getting to the 7zip help site. The -aoa switch overwrites. The -aou renames the extracted file if it already exists in the target directory. The -aot renames the existing file in the target directory.Denis
Sorry, I meant this website which seems to have a more thorough answers. sevenzip.osdn.jp/chm/cmdline/commands/rename.htm the 7zip website is just similar to the help docs. If using -aou, where do I specify what I want the file to be called?Aryan
You don't. The help clearly states it will add _1 to the base file name depending on which switch you use.Denis
C
9

I was searching for a solution myself. Of course, extracting a file to a completely different filename only makes sense if you're only extracting one file.

What I ended up doing was to extract to standard out (-so) and redirecting the output to the desired name:

  7z e my-compressed-file.7z -so readme.txt > new-filename.txt
Cassiopeia answered 8/11, 2019 at 18:47 Comment(2)
no need to put "readme.txt" there. the command 7z e -so file.7z > newfile.ext works nicely ;) and thanks for that! really did the trick here!Fig
Unfortunately redirecting the file via standard output results in a file with current timestamp instead of original timestamp.Preadamite
D
2

Made some tweaks to Mark's scripts. For some more advanced use.

for %%f in (folder\*.7z) do (7z e "%%f" -so -r *filename1*.jpeg *filename2*.jpg > tmp\%%f.jpg)
  • now loops through multiple source files for %%f in (folder\*.7z)
  • wildcard for the source file with optional sub-folder folder\*.7z
  • multiple specific extracted files with wildcards *filename1*.jpeg *filename2*.jpg
  • searches inside the source file recursively -r
  • output file is named the same as input source file with optional sub-folder tmp\%%f.jpg
Dittman answered 22/2, 2021 at 18:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.