Creating a logical folder structure when creating a 7-Zip archive via the command-line

If you simply want to zip up everything in the folder, then you could do this it will maintain folder structure.

."C:\Program Files\7-Zip\7z.exe" a -mx9 "C:\_test\20210112 Testing.7z" "C:\_test\*"

EDIT: To selectively archive certain files, use relative paths. In your case, you could set C:\_test as your current directory and then run

."C:\Program Files\7-Zip\7z.exe" a -mx9 "C:\_test\20210112 Testing.7z" "Data\Testing\data.txt" "Logs\Testing\data.txt"

Interesting question. I'm using 7z regularly, but never did I need such a workflow.

As you know you are getting the error due to the fact that in the archive you have duplicate file in flat structure. The 7z archiver does not know which one to used, thus the error.

I think you were quite close, but the issue is that you are using fully classified path to access the files for packing.


Your solution is to run the 7z from C:\_test\:

"C:\Program Files\7-Zip\7z.exe" a -mx9 -spf2 "C:\_test\20210112_Testing.7z" "Data\Testing\data.txt" "Logs\Testing\data.txt"

Now your archive will have the different path for the same file name. I also recommend not using space in the 7z file name - you will save yourself troubles in the future. The -spf2 switch will adjust if you have relative or absolute path based on the input. I tend to have it there if I should need to mix it.


I like to use the -spf2 (does not include the drive letter in the full path) switch as that makes life easier when switching among OS.