Page 1 of 1

"save" directory or file ?

Posted: 03 Jul 2014 13:23
by JDługosz
I've never done anything with XYplorer's built-in scripting language.
So maybe someone could help me automate this:

Operate on the selection in the list pane. If the name ends in "-SAVE" then copy it to the un-saved name. For example, "foo-SAVE" will be copied to "foo", so now I have "foo" and "foo-SAVE" with identical contents. foo is a directory in my case now, but it would be great if it worked for files and directories equally well.

If the name does not end in "-SAVE", then rename it so that it does. "foo" becomes "foo-SAVE".

It should work with multiple selections. E.g. I ctrl-click two directories in the list pane.

Also, given such a script, what's the easiest way to invoke it? I'm not committed to that naming convention--that's just what I'm using at the moment. So if it were "foo.SAVE" instead, a file-extension association with *.SAVE could do the restore operation. But it would need to use a different method to SAVE it, and that might be confusing. Just thinking...

Re: "save" directory or file ?

Posted: 03 Jul 2014 14:23
by highend

Code: Select all

    foreach($item, "<get SelectedItemsPathNames |>") {
        if (exists($item) == 2) { $baseName = getpathcomponent($item, "file"); $ext = ""; } // Folder
        else { $baseName = getpathcomponent($item, "base"); $ext = "." . getpathcomponent($item, "ext"); } // File
        if (regexmatches($baseName, "-SAVE$") != "") { // Contains "-SAVE"
            $newName = regexreplace($baseName, "-SAVE$");
            copyitem $item, "<curpath>\" . $newName . $ext;
        } else { // Does not contain "-SAVE"
             renameitem($baseName . "-SAVE" . $ext, $item);
        }
    }
Test it...

Re: "save" directory or file ?

Posted: 09 Jul 2014 06:07
by JDługosz
highend wrote: Test it...
Thanks. I pasted it in the edit box when creating an entry for the User menu. When I select the new command, it pops up another menu showing "foreach($item, "<get SelectedItemsPathNa…" on one line and just "}" on another. It seems to be a menu because of how the highlight follows the cursor between the two lines.

I'm confused.

Re: "save" directory or file ?

Posted: 09 Jul 2014 08:12
by highend
Show a screenshot of where you tried to paste it. You either removed the indentation or put it into a single line edit box.

Re: "save" directory or file ?

Posted: 10 Jul 2014 08:46
by JDługosz
highend wrote:Show a screenshot of where you tried to paste it. You either removed the indentation or put it into a single line edit box.
000350.png

Re: "save" directory or file ?

Posted: 10 Jul 2014 09:42
by highend
As you can see the "foreach" and the closing "}" are on the same column 1.

Just put at least one space / tab before both of them.

Re: "save" directory or file ?

Posted: 10 Jul 2014 18:27
by JDługosz
Thanks... that's less than intuitive, I must say.

Re: "save" directory or file ?

Posted: 11 Jul 2014 07:44
by bdeshi
That's a syntax rule for XYscripts.
To read more, enter this in the addressbar of XY:

Code: Select all

::help "idh_scripting.htm#idh_scripting_multi";

Re: "save" directory or file ?

Posted: 13 Jul 2014 04:15
by JDługosz
I had looked through the script docs a little, to see where I would put a script. Ended up seeing that User Command had a blank for that, so it stashed it away someplace.

I thought the indent thing was specifically for script files.