19

I have a bunch of files like this:

C:\G\G1\35antique-shop.mp3
C:\G\G2\35antique-shop.mp3
C:\G\G1\09saguri.mp3
C:\G\G2\09saguri.mp3

I just want to know how to keep the folder structure while archiving. This is more important than the duplicate thing, to know how to solve that, would be a bonus.

@Lamb "Do you want to archive only selective files (not the whole folder content) from the G1 and G2 folders?"

That almost says it all, but I'll try to improve: Not only do I want to move only some selected files to an archive. I also have a list of these files, including path-names to each file.

So, in SubDir1 there might be 10 files, of which I want to zip three, and in SubDir1\sub2 there might be five files, of which I want to zip all five. The files that I need to archive are in a listfile.

There might be duplicate files, so my hope is that with subdirectories added, 7-Zip does NOT see them as duplicates.

And I like to do it on the command line.

I've tried a batch file like this:

7z a -r MyArchive.7z C:\G\G1\35antique-shop.mp3
7z a -r MyArchive.7z C:\G\G2\35antique-shop.mp3
7z a -r MyArchive.7z C:\G\G1\09saguri.mp3
7z a -r MyArchive.7z C:\G\G2\09saguri.mp3

This also means that files 01antique-shop.mp3, 02antique-shop.mp3, ...., til 33antique-shop.mp3 and 34antique-shop.mp3, should NOT be archived.

The foldernames (=subdirectories) weren't added to MyArchive.7z, and this is the problem.

Obviously, I'd like to use just one commando with a listfile, but that doesn't work either.

I've tried:

7z a -mx0 -tzip C:\$$-edrive\F-G-H.zip @fgh.txt

which actually works pretty fine as far as the selecting goes, but it does NOT keep the directory structure. Sadly.

How do I do this?

  • Do you want to archive only selective files (not the whole folder content) from the G1 and G2 folders ? – Ankit Feb 02 '13 at 16:45

6 Answers6

17

The latest stable version of 7-Zip (15.14) has the switch -spf that enables the absolute path storage.

For example, if the file list.txt is

C:\tmp\dir1\file.txt
C:\tmp\dir2\file.txt

the command 7z a p.7z -spf @list.txt will produce an archive storing the absolute paths as is in the list. Alternatively, the command 7z a p.7z -spf2 @list.txt will remove the drive letter:

tmp\dir1\file.txt
tmp\dir2\file.txt

If the file list.txt is

dir1\file.txt
dir2\file.txt

the command 7z a p.7z @list.txt will store the relative paths as is in the list, in both versions 9.20 and 15.14.

I did more tests with the switch -spf. The results are here.

Marco
  • 493
10

I got it to archive a directory with subdirectories using the following:

7z.exe a - t7z NewArchivePath PathOfFolderToArchive
Eric Roper
  • 346
  • 1
  • 3
  • I can't find a direct link to the 7zA.exe anywhere (as per your link), is it obsolete and replaced by 7z.exe? – PrincessPooleRulz Feb 02 '13 at 18:26
  • @user194767: 7za.exe is the command-line version. – Karan Feb 02 '13 at 19:50
  • and, I'd like to use it, but I can't find a like to it on that page, 7-zip.org/download.html. So, just to be clear: with "can't find a direct link to the 7zA.exe anywhere" I really meant: "Please give me a direct link to this free software!" Thank you! – PrincessPooleRulz Feb 02 '13 at 20:34
  • 1
    @user194767: Ok, here's the current version, but are you telling me you couldn't spot 7-Zip Command Line Version anywhere on that page?! – Karan Feb 02 '13 at 20:51
  • I couldn't 7zA.exe so tried using 7z.exe and it worked. I'm not sure what the -r flag is used for but it is not needed to archive subdirectories. – Eric Roper Feb 02 '13 at 21:55
  • @Karan, aha thank you. What with my headless chicken impression and all that, I didn't realize that the already downloaded 7za920.zip contained the 7za.exe, since I was looking for a direct link to 7za.exe, like 7-zip.org/7za.exe or something similar. I don't know why I did that. But thanks all the same. – PrincessPooleRulz Feb 02 '13 at 23:01
  • It should be -t7z instead of - t7z – sobi3ch Jan 28 '22 at 07:55
7

Try this instead:

cd /d C:\
7z a MyArchive.7z G\G1\35antique-shop.mp3
7z a MyArchive.7z G\G2\35antique-shop.mp3
7z a MyArchive.7z G\G1\09saguri.mp3
7z a MyArchive.7z G\G2\09saguri.mp3

The problem seems to be that people have been requesting for years for proper path storage options (just as they have for the ability to modify multi-volume archives), but the author doesn't seem to be interested. That's part of the reason why I started using WinRAR so much.


If you have a listfile, 7-Zip will not store paths if the files to be archived do not share a single top-level directory. For example if you have Input.txt containing the following:

C:\path to\file1.ext
D:\path to\file1.ext
E:\path to\file1.ext

If you run 7z a MyArchive.7z @Input.txt, you'll see that the paths have been stripped completely, which seems completely backwards to me (OTOH rar a MyArchive.rar @Input.txt does the job just fine).

So to overcome 7-Zip's deficiency, you can use a batch file as follows:

for /f "tokens=1* delims=\" %%i in (Input.txt) do (
    pushd %%i\
    7z a D:\MyArchive.7z "%%j"
    popd
)

Note: Replace D:\MyArchive.7z with the proper path to your destination archive. Also, the batch file should work irrespective of whether you have quoted paths in your listfile or not.


Edit: If your listfile looks like:

"C:\$$-edrive\F\faithless.-.long.way.mp3"
"C:\$$-edrive\G\Antique Shop.mp3"

or

"\$$-edrive\F\faithless.-.long.way.mp3"
"\$$-edrive\G\Antique Shop.mp3"

then edit it to look like this instead:

"$$-edrive\F\faithless.-.long.way.mp3"
"$$-edrive\G\Antique Shop.mp3"

Now assuming all files to be archived are under C:\$$-edrive, navigate to C:\ and run 7z a C:\MyArchive.7z @C:\Input.txt. Using a listfile the paths will be saved only if no drive letters are present, no initial backslashes are there and if the top-level (root) directory is the same for all files. Also, for best results I advise you not to create MyArchive.7z in C:\$$-edrive.

Karan
  • 56,477
  • Well, the answer was 3/4 ok. RAR worked super,but batch file worked soso.

    Here's the output, and it only added ONE file to my archive, the one without spaces in the filename___

    C:\Programs\zippers\7-Zip>( pushd "C:
    c:\Programs\zippers\7-Zip\7z.exe a c:$$-edrive\FGH.7z "$$-edrive\G\gattaca_7mp 3_zefen\gattaca - 03 the one moment.mp3" " popd )

    7-Zip 4.65 Copyright (c) 1999-2009 Igor Pavlov 2009

    Scanning

    Updating archive c:$$-edrive\FGH.7z

    Error: Can not open file FGH.7z.tmp Access denied.

    System error: undefined error=====

    Most important is perhaps the access denied error?

    – PrincessPooleRulz Feb 03 '13 at 02:04
  • Can you add a few sample lines (with spaces and without) from your actual listfile to your question above? – Karan Feb 03 '13 at 02:14
  • okay, so I replaced all the spaces with dots in the filenames, but not in the pathnames, which I will try now.

    output is still: C:\Programs\zippers\7-Zip>(______ pushd c:_______ c:\Programs\zippers\7-Zip\7z.exe a c:$$-edrive\FGH.7z "$$-edrive\F\filmmusic_14mp3_foretean\starcraft.-.starcraft.vs.south.park.mp3"_______ popd________ )________

    Scanning

    Updating archive c:$$-edrive\FGH.7z

    Error: Can not open file FGH.7z.tmp Access denied.

    System error: undefined error

    – PrincessPooleRulz Feb 03 '13 at 02:28
  • "I replaced all the spaces with dots in the filenames" - Meaning you renamed the actual files, or just edited the listfile? Spaces in both filenames and paths should be fine, see my edit above (i.e. from your listfile remove C:\ and place quotes around all paths). Now you should be able to use 7z directly instead of my batch file workaround. – Karan Feb 03 '13 at 02:34
  • I renamed both = = = = = = 2. I've made your changes, but it still doesn't work with 7z = = = = = =3. $$-edrive\ is indeed the only top directory. = = = = = 4. Error messages = = = = = WARNINGS for files:
  • = = = = = $$-edrive : The system can't find that file. = = = = = = = = = = "$$-edrive : The system can't find that file. = = = = =

    = = = = = WARNING: Cannot find 2 files = = = = =

    C:\Programs\zippers\7-Zip> = = = = =

    note that there are two warnings, and the second one starts with one double quote... Odd?

    – PrincessPooleRulz Feb 03 '13 at 02:51
  • "$$-edrive : The system can't find that file" - Isn't it a directory? Did you add the quotes? Did you run the command from C:\ (i.e. the root, *not* from C:\Programs\zippers\7-Zip)? Is $$-edrive a normal dir or some sort of network mapped deal? – Karan Feb 03 '13 at 02:53
  • okay so here's the deal: If I edit the "C:" out of the listfile, I get an error telling that the system can't find the file. _______________________________But if I use the listfile, with the "C:", then the files are archived but withhout a a path. that's back to square one ...

    PS i ran the command from a batch file from the $$-edrive folder with the full path for 7z edited into that batchfile.

    – PrincessPooleRulz Feb 03 '13 at 03:24
  • So the paths were like "$$-edrive\G\Antique Shop.mp3", and you ran from C:$$-edrive? Can you please run from C:\ instead, as I've mentioned twice already above? – Karan Feb 03 '13 at 03:40
  • $$-edrive is a normal dir. – PrincessPooleRulz Feb 03 '13 at 03:42
  • i moved both batch file and listfile to another dir, but it still gave the same err message: Can't find that file. and when I add " C:" it doesn't keep the dir structure. – PrincessPooleRulz Feb 03 '13 at 03:58
  • Ok, last time before I have to leave: 1. Remove C:\ from listfile. 2. Ensure file paths have quotes. 3. Go to C:. Cannot emphasise this enough. C:. Root dir. Not a sub-dir. 4. Run 7z a D:\MyArchive.7z @D:\Input.txt directly from the command prompt (yeah, for best results don't mix the new archive in the same dir where the files to be archived are in). 5. I have tried lots of times by now and it does work (with 7za.exe at least). You can confirm and then add to a batch file. 6. There, done; am off for now. Hopefully when I check back I will see a check mark next to my answer! :P – Karan Feb 03 '13 at 04:03
  • nope, it don't work. no dir structure. – PrincessPooleRulz Feb 03 '13 at 04:26
  • so, i ran it from the root, but still nothing. I guess im swithching to rar, which kinda suckx – PrincessPooleRulz Feb 03 '13 at 14:23
  • I don't know why you'd say RAR sucks. Sure it's not free, but on the whole I find it much better in terms of features compared to 7-Zip, and the compression levels are comparable. Also, not to sound condescending, but if my answer's not working for you then you're probably doing something wrong. Here's proof that it does work, and I have even replicated your fgh.txt listfile and confirmed. Can't say much more than that now, can I, so it's time to move on to something else... – Karan Feb 03 '13 at 18:22
  • Worked good for me. It does take a long time though. – Todd Partridge Jan 24 '24 at 17:47