zipping files with the same name in different folders using 7z @listfile feature

jockster picture jockster · Oct 1, 2012 · Viewed 13k times · Source

I want to create a 7zip file containing files with the same names but in different folders using 7zip's @listfile feature. Although I have used 7zip CLI for a long time, I just cannot find the syntax to accomplish this.

My file tree looks like this (note that somefile1.html and somefile2.html occur twice each).

|   somefile2.html
+---dir1
|       somefile1.html
|       somefile2.html
|       
+---dir2
|       somefile3.html
|       somefile4.html
|       
\---dir3
        somefile1.html
        somefile5.html

Using a @listfile works fine, but I cannot figure out how to retain the directory tree in the resulting 7zip while doing that.

I have tried to the following syntax:

7z a [email protected] my_compressed_file.7z

Then, given the explanations in 7zip's Windows CHM help file under syntax, I tried including the root folder, too (called 'files'):

7z a [email protected] my_compressed_file.7z .\files

I get the same error in both cases:

Error
Duplicate filename:
somefile1.html
somefile2.html

Has anyone figured this one out and would care to shed some light on it?

I know how to compress files with the same names in different folders otherwise (when the folder structure is retained in the 7zip it s no problem). But this time the few files I need are spread all over the place...

Answer

bart picture bart · May 6, 2013

I ran into the same error (Duplicate filename) while using a list file. My list file looked like this:

D:\SomeDir\SomeFile.txt
D:\SomeOtherDir\SomeFile.txt

I resolved the problem by launching 7-zip from D:\ and editing my list file to use relative rather than absolute paths:

SomeDir\SomeFile.txt
SomeOtherDir\SomeFile.txt

The resulting archive preserved the original directory structure and looked exactly as I expected.

Note: I was using 7-Zip 4.65, and this solution will not work if the files are on different drives or have a different root.

The PowerShell command line I ran was:

& 'C:\Program Files\7-Zip\7z.exe' a -scsWIN logs.7z `@recentlogs.txt