XYplorer: Create a Time Stamped ZIP Folder

Something I find I need to do quite regularly is take a snap shot of a folder. For the purposes of saving space (and often for passing on the folder to someone else) I like to compress the folder into a ZIP archive and prefix the current time stamp to the file name.

This little activity was perfect for a bit of automation and so I created a little script in XYplorer (my Windows file manager of choice) to make this as easy as clicking a button.

How it Works

Originally the script started as something that would just ZIP the selected folder, but over time it became slightly more sophisticated.

XYplorer’s scripting language includes a single command that is capable of zipping and also standard variables for working with current file paths, dates and date formats. The basic operation of creating a time stamped ZIP was therefore trivial.

e.g. zip\_add("\<curpath\>\<date yyyy-mm-dd-hh.nn.ss\>\<curbase\>.zip");

Creates a ZIP file for the current selection using its base file name (i.e. no file extension), prefixing the current time stamp and saving it to the parent folder of the selected file/folder.

I extended the script a little based around what I had selected. If I didn’t select a single folder to ZIP, the result I wanted was to ZIP the parent folder instead.

That comes down to the following cases:

  • If I selected a single folder, then the folder would be ZIP’d as described above.
  • If I selected a single file, then the folder containing that file (i.e. the parent folder) would be ZIP’d and placed alongside the folder.
  • If I selected multiple files/folders, then again the folder containing the selected files would be ZIP’d and placed alongside the folder.
  • If I selected no files/folders, then again the folder containing the selected files would be ZIP’d and placed alongside the folder.

The Script

In terms of scripting this therefore I set a flag (the $ZipSelected variable). If the flag is true, the script ZIPs up the currently selected folder. If the flag is false, the script ZIPs up the parent folder.

The flag is initially defaulted to false, but a check of the number of selected items being one and the selected item being a folder overrides this and sets it to true.

A notable line in the script is the setting of $parentFolder. Whatever XYplorer pane I’m working in populates the folder path into the special variable “<curpath>”. The parent folder into which the ZIP of that folder should be placed is then one folder up from that. I couldn’t find an easy way to get the parent, so I used the getpathcomponent function to get the number of path components, subtract one from it and then return that many components from <curpath>, again using getpathcomponent; but using the “component” parameter rather than the “count” parameter.

getpathcomponent is also used to return just the last component of <curpath>, and together these can be used to specify the

"ZIP a Directory & Prefix Current Timestamp"
    $ZipSelected = false;
    if (get("countselected") == 1) {
        if (exists(<selitem>) == 2) {
            $ZipSelected = true;
        }
    }

    if ($ZipSelected == true) {
        //Only one selection and it is a folder, so zip it
        zip_add("<curpath>\<date yyyy-mm-dd-hh.nn.ss> <curbase>.zip");
    }
    else {
        //Multiple things selected or not a single folder, so ZIP the whole current folder into the parent folder
        $parentFolder = getpathcomponent(<curpath>, "component", getpathcomponent(<curpath>, "count") - 1, 1);
        $currentFolder = getpathcomponent(<curpath>, "component", -1);
        zip_add("$parentFolder\<date yyyy-mm-dd-hh.nn.ss> $currentFolder.zip", "<curpath>");
    }
Author: Stephen Millard
Tags: | scripting | xyplorer |

Buy me a coffeeBuy me a coffee



Related posts that you may also like to read