Page 1 of 1

How to archive directories with a user command

Posted: 13 Mar 2015 18:08
by Sander Bouwhuis
I'm trying to archive individual directories with 7z for backup purposes, but can't get it working.

Let's pretend we have the following dirs:
Z:\SomeDir\Documents
Z:\SomeDir\Pictures
Z:\SomeDir\Music

In order to archive the dirs individually I need to supply the following commands:
7z a -mx0 "Z:\SomeDir\Documents.7z" "Z:\SomeOtherDir\Documents\*"
7z a -mx0 "Z:\SomeDir\Pictures.7z" "Z:\SomeOtherDir\Pictures\*"
7z a -mx0 "Z:\SomeDir\Music.7z" "Z:\SomeOtherDir\Music\*"

I tried the following:
User commands/Open With
"C:\Utilities\7-Zip\7z.exe" a -mx0 "<curitem>.7z" "<curitem>\*"
Multiple instance (pass each of the items to its own instance of the application).

It doesn't work. Does anyone know how to fix this?
Also, as a bonus question: is there a way to make sure ONLY selected directories are taken into consideration?

Many thanks in advance for the help.

Re: How to archive directories with a user command

Posted: 13 Mar 2015 18:37
by highend
Select the folders and then use a script:

Code: Select all

    $packer = "D:\Tools\7-Zip\7z.exe";

    foreach($folder, "<get SelectedItemsPathNames |>") {
        if (exists($folder) == 2) {
            run """$packer"" a -mx0 ""$folder.7z"" ""$folder\*""", , 2, 1;
        }
    }
Ofc you have to change the path to your 7z.exe...

Re: How to archive directories with a user command

Posted: 13 Mar 2015 18:42
by bdeshi
beat me.

btw, you can still use the User menu: just place this script in the Run Script category

Re: How to archive directories with a user command

Posted: 13 Mar 2015 18:53
by Sander Bouwhuis
Hmmm... when I try to run the script it shows me a popup where I have two choices:

$packer = "D:\Tools\7-Zip\7z.exe";
foreach($folder, "<get SelectedItemsPath...

Why do I need to choose?

Edit:
The first one does nothing.
The second one gives an error : "The system cannot find the specified file"

Re: How to archive directories with a user command

Posted: 13 Mar 2015 19:07
by bdeshi
there are a lot of spaces before each line. Keep them intact.

Re: How to archive directories with a user command

Posted: 13 Mar 2015 19:32
by Sander Bouwhuis
Wow, you are amazing! It works wonderfully!

Two last questions.

1.
Does the 7z.exe path have to be in a variable, or can I just call it directly?

2.
Is it possible to go into the directory before starting the archiving, and then return to the parent folder after archiving?
This is what I mean:

Code: Select all

        $packer = "D:\Tools\7-Zip\7z.exe";

        foreach($folder, "<get SelectedItemsPathNames |>") {
            if (exists($folder) == 2) {
                cd $folder
                run """$packer"" a -mx0 ""$folder.7z"" ""*""", , 2, 1;
                cd..
            }
        }

Re: How to archive directories with a user command

Posted: 13 Mar 2015 20:18
by highend
1
Ofc you can call it without assigning it to a variable before.
2
Why would you want to do that? Apart from the thing that your active pane would be switching back and forth all selected folders...

Re: How to archive directories with a user command

Posted: 14 Mar 2015 09:27
by bdeshi

Code: Select all

                cd $folder
                run """$packer"" a -mx0 ""$folder.7z"" ""*""", , 2, 1;
                cd..
to

Code: Select all

                goto $folder;
                run """$packer"" a -mx0 ""$folder.7z"" ""*""", , 2, 1;
                wait 2000; //let's take rest here for a couple seconds, look around, see stuff...
                goto "$folder\..\";

Re: How to archive directories with a user command

Posted: 14 Mar 2015 09:59
by Sander Bouwhuis
I want to do this because when I 7z the folder, I only want the files in the folder to be archived.
If I don't go into the folder, the archive will have a superfluous root folder.

Why do we need the 2 second wait?
There are 362 directories I'm backing up, which means 724 seconds of waiting.
The archiving itself takes about a second per directory.

Edit:
Great news! It works like a charm. The waiting is not necessary I think. I left it out and it SEEMS to work.
What are the problems I could encounter if I don't wait 2 seconds?

This is the final script, for anyone interested. I adjusted it such that only the files and directories in the selected directory are archived.
A HUGE thanks goes out to highend and sammysarkar for the help.

Code: Select all

        foreach($dir, "<get SelectedItemsNames |>")
        {
          $path = get("path");
          if(exists("$path\$dir") == 2)
          {
            run """C:\Utilities\7-Zip\7z.exe"" a -r -mx0 ""$dir.7z"" "".\$dir\*""", , 2, 1;
          }
        }

Re: How to archive directories with a user command

Posted: 14 Mar 2015 13:08
by bdeshi
"Why do we need the 2 second wait?" - No good reason. I just thought you'd wanted to look into the folder to see if everything's gone according to plan. :lol: