Scripting question: Zipping up one or more files

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
avsfan
Posts: 554
Joined: 29 Jun 2006 09:00
Location: Fort Collins, Colorado

Scripting question: Zipping up one or more files

Post by avsfan »

Hi,

I haven't tried any scripting yet, but as I started looking at what was available, I didn't see any way to do what I want to do. So I'll ask to see if I'm missing something...

What I'd like to do is focus one or more items in the list, and then press a CKS and have the following happen:

1) if a single file is selected, run "zip -mT <basename>.zip <selectedFile>"
2) if multiple files are selected, ask the user for the zip basename, and then execute the above zip command using the new basename.

In other words, if I have a file named "myInstallFile.exe" I'd like to select it, press the custom key combination, and have it move that file into myInstallFile.zip. (using the -m option of the InfoZip "zip" command).

Is there a way to accomplish this? This would make things VERY nice!

Thanks!

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Scripting question: Zipping up one or more files

Post by TheQwerty »

avsfan wrote:Is there a way to accomplish this?
Short answer: No.

Long answer: There's currently no conditional logic in XY scripting, so you can't have it do something different based on the number of selected files.

There are other ways to do this though.

You could always prompt the user for the basename using the current basename as a default. If there's 1 item the user just has to hit enter. If there's more than 1 they can enter a different basename.

You could create two scripts or UDCs; one for single items and another for multiple items.

You could call another program with the selection from XY that would be in charge of doing all the heavy lifting.


I unfortunately don't have the time to give examples right now, but I'll try to add some if no one else chimes in.

jacky
XYwiki Master
Posts: 3106
Joined: 23 Aug 2005 22:25
Location: France
Contact:

Post by jacky »

It's possible to make what you want for single item selected, yes, and for multiple items selected, yes. But you cannot do a script that will test how many items are selected and acts accordingly, no.

Your solution is either :
- Use two scripts, one for each case, and on KS for each. Or,
- Use one script that will almways ask for the basename, but use the focused item's basename as default value, so for single item selected you'll only have to press one more key : Enter (not to mention the ability to actually tweak the name ;))

I don't know/use InfoZIP myself, but here's what I would think should work, based on what you said:

Code: Select all

input $base, "Enter the basename for the new archive", <curbase>; openwith """zip"" -mT ""$base.zip"" <items>";
Edit: Okay, so TheQwerty replied while I was typing this, and we agree so it's all good ;)
Proud XYplorer Fanatic

avsfan
Posts: 554
Joined: 29 Jun 2006 09:00
Location: Fort Collins, Colorado

Post by avsfan »

jacky wrote:(snip)
- Use one script that will almways ask for the basename, but use the focused item's basename as default value, so for single item selected you'll only have to press one more key : Enter (not to mention the ability to actually tweak the name ;))

I don't know/use InfoZIP myself, but here's what I would think should work, based on what you said:

Code: Select all

input $base, "Enter the basename for the new archive", <curbase>; openwith """zip"" -mT ""$base.zip"" <items>";
This looks like *almost* what I need. The only problem with this is that the <items> have the full path name in the zip file. For the case I'm interested in, I would like to have all of the pathnames be relative to the folder I'm working in (i.e. no subdirectories in the zip file). I can probably make this work using a zip option to ignore directory names, but it'd be nice to be able to figure out a way to do it from XY.

Lastly (and I feel really dumb asking this question, as I thought it would be obvious, but can't seem to find it) -- how would I set this up to be called by a keyboard shortcut? I was able to test it by just using Jacky's code above and prefixing it with a "::" in the address line, but I haven't figured out how to call this from a keystroke. Any help will be much appreciated!

Thanks for helping me get my feet wet on this -- there's GREAT potential and power in scripting!

Thanks again!

admin
Site Admin
Posts: 60531
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Post by admin »

avsfan wrote:
jacky wrote:(snip)
- Use one script that will almways ask for the basename, but use the focused item's basename as default value, so for single item selected you'll only have to press one more key : Enter (not to mention the ability to actually tweak the name ;))

I don't know/use InfoZIP myself, but here's what I would think should work, based on what you said:

Code: Select all

input $base, "Enter the basename for the new archive", <curbase>; openwith """zip"" -mT ""$base.zip"" <items>";
This looks like *almost* what I need. The only problem with this is that the <items> have the full path name in the zip file. For the case I'm interested in, I would like to have all of the pathnames be relative to the folder I'm working in (i.e. no subdirectories in the zip file). I can probably make this work using a zip option to ignore directory names, but it'd be nice to be able to figure out a way to do it from XY.

Lastly (and I feel really dumb asking this question, as I thought it would be obvious, but can't seem to find it) -- how would I set this up to be called by a keyboard shortcut? I was able to test it by just using Jacky's code above and prefixing it with a "::" in the address line, but I haven't figured out how to call this from a keystroke. Any help will be much appreciated!
Keystroke: simply add your script as UDC / Run Script, then apply a KS to it.

Your wish: conditional logic will come in scripting 2.0... :)

avsfan
Posts: 554
Joined: 29 Jun 2006 09:00
Location: Fort Collins, Colorado

Post by avsfan »

admin wrote: Keystroke: simply add your script as UDC / Run Script, then apply a KS to it.

Your wish: conditional logic will come in scripting 2.0... :)
Ah, of course -- I somehow completely missed the "User" Menu this time and was only looking at the Tools->Customize Keyboard Shortcuts (which, of course, didn't help much!). The funny thing is, I have assigned commands in the past for WinMerge, etc, and had completely forgotten how I did it!

So, now that I have fixed that much, is it possible to have <items> have only the relative name (without path)? Or do I need to have the zip application strip the paths? It seems to me that there are likely to be cases where having just the relative names rather than the full names would be desirable (though certainly in many cases the full names are useful as well). Any chance of a <localitems> or <relativeItems> or <items,nopath> in the future?

Thanks for the quick reply!

admin
Site Admin
Posts: 60531
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Post by admin »

avsfan wrote:
admin wrote: Keystroke: simply add your script as UDC / Run Script, then apply a KS to it.

Your wish: conditional logic will come in scripting 2.0... :)
Ah, of course -- I somehow completely missed the "User" Menu this time and was only looking at the Tools->Customize Keyboard Shortcuts (which, of course, didn't help much!). The funny thing is, I have assigned commands in the past for WinMerge, etc, and had completely forgotten how I did it!

So, now that I have fixed that much, is it possible to have <items> have only the relative name (without path)? Or do I need to have the zip application strip the paths? It seems to me that there are likely to be cases where having just the relative names rather than the full names would be desirable (though certainly in many cases the full names are useful as well). Any chance of a <localitems> or <relativeItems> or <items,nopath> in the future?
Good question. Currently it is like this: When the command line would get longer than 2047 characters then automatically <items> is filled with names without path, else the path is included.

This can be improved, maybe by using one of your ideas. But later... currently I have no head for this.

avsfan
Posts: 554
Joined: 29 Jun 2006 09:00
Location: Fort Collins, Colorado

Post by avsfan »

admin wrote:
avsfan wrote:
admin wrote: Keystroke: simply add your script as UDC / Run Script, then apply a KS to it.

Your wish: conditional logic will come in scripting 2.0... :)
Ah, of course -- I somehow completely missed the "User" Menu this time and was only looking at the Tools->Customize Keyboard Shortcuts (which, of course, didn't help much!). The funny thing is, I have assigned commands in the past for WinMerge, etc, and had completely forgotten how I did it!

So, now that I have fixed that much, is it possible to have <items> have only the relative name (without path)? Or do I need to have the zip application strip the paths? It seems to me that there are likely to be cases where having just the relative names rather than the full names would be desirable (though certainly in many cases the full names are useful as well). Any chance of a <localitems> or <relativeItems> or <items,nopath> in the future?
Good question. Currently it is like this: When the command line would get longer than 2047 characters then automatically <items> is filled with names without path, else the path is included.

This can be improved, maybe by using one of your ideas. But later... currently I have no head for this.
Excellent! This will be very nice to have when you get to it. In the meantime, there is, in fact, a command-line switch that allows zip to "junk pathnames" so I think I'm all set!

thanks again!

Post Reply