How to archive directories with a user command

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
Sander Bouwhuis
Posts: 249
Joined: 10 Jun 2008 15:40
Location: Netherlands

How to archive directories with a user command

Post 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.
(\__/)
(='.'=) This is Bunny. Copy and paste bunny into
(")_(") your signature to help him gain world domination.

highend
Posts: 14942
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: How to archive directories with a user command

Post 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...
One of my scripts helped you out? Please donate via Paypal

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: How to archive directories with a user command

Post by bdeshi »

beat me.

btw, you can still use the User menu: just place this script in the Run Script category
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

Sander Bouwhuis
Posts: 249
Joined: 10 Jun 2008 15:40
Location: Netherlands

Re: How to archive directories with a user command

Post 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"
(\__/)
(='.'=) This is Bunny. Copy and paste bunny into
(")_(") your signature to help him gain world domination.

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: How to archive directories with a user command

Post by bdeshi »

there are a lot of spaces before each line. Keep them intact.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

Sander Bouwhuis
Posts: 249
Joined: 10 Jun 2008 15:40
Location: Netherlands

Re: How to archive directories with a user command

Post 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..
            }
        }
(\__/)
(='.'=) This is Bunny. Copy and paste bunny into
(")_(") your signature to help him gain world domination.

highend
Posts: 14942
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: How to archive directories with a user command

Post 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...
One of my scripts helped you out? Please donate via Paypal

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: How to archive directories with a user command

Post 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\..\";
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

Sander Bouwhuis
Posts: 249
Joined: 10 Jun 2008 15:40
Location: Netherlands

Re: How to archive directories with a user command

Post 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;
          }
        }
(\__/)
(='.'=) This is Bunny. Copy and paste bunny into
(")_(") your signature to help him gain world domination.

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: How to archive directories with a user command

Post 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:
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

Post Reply