1

I tried to make a custom command that unzip a selected file to a path defined by user, but with a default value to current path and current archive name in remote server using this command, but the prompt just gave me an empty value. What's the mistake?

unzip "!" -d "!?&Extraction Path:?!/!!"

Thanks in advance!

1 Answer 1

1

It's not possible. There's actually a feature request for this functionality:
Bug 743 – Allow patterns in default prompt answer in custom commands.

Though even that it meant to support only static (non-file) patterns like !/, but not file patterns like !.


If it helps, in WinSCP extensions, it's possible to use non-file patterns, like !/ (but not file patterns, like !) in default prompt/option answer.

The extension file may look like:

@name     Unzip...
@side     Remote
@command  unzip "!" -d "%ExtractionPath%"
@option   ExtractionPath -run textbox "Extraction path:" "!/"

Just store the above script to a text file and install it to WinSCP.


Another thing that you can do, is to add a checkbox that will make WinSCP add an archive name (without an extension) to the path, with some clever use of shell (bash) constructs. This way, you can uncheck the checkbox and add a custom subfolder to the target path manually, if you do not want to use the archive name for the subfolder name.

@name     Unzip...
@side     Remote
@command  unzip "!" -d "%ExtractionPath%`[[ '%AddName%' = '1' ]] && AN=! && echo ${AN%.*}`"
@option   ExtractionPath -run textbox "Extraction path:" "!/"
@option   AddName -run checkbox "Add file name to the extraction path" "1" "1"

enter image description here


Yet another alternative is to use your own placeholder for archive name (e.g. ARCHIVENAME) that will get replaced by real name (without an extension), when the command is executed. Then, if you do not want to use the archive name for the subfolder name, you replace the ARCHIVENAME with a custom name.

@name     Unzip...
@side     Remote
@command  unzip "!" -d "`EP=%ExtractionPath%;AN=!;AN=${AN%.*};echo ${EP/ARCHIVENAME/$AN}`"
@option   ExtractionPath -run textbox "Extraction path:" "!/ARCHIVENAME"

enter image description here

5
  • 1
    That last method is genius! Thank you for the very thorough answer! Nov 13, 2018 at 12:30
  • Hey I try to modify the last command a little bit to make it remove the file extension by using this command @command unzip "!" -d "``EP=%ExtractionPath%;echo $(${EP/ARCHIVENAME/!} | awk -F. '{ print $1 }')``" and WinSCP hanged on executing command and give me an error saying Host is not communicating..., any idea why? PS: dont worry about the extra `` mark, it's the formatting which I don't know how to fix yet. Nov 13, 2018 at 13:14
  • Ah okay, let's say I have an archive named archive.zip, using the command in last method would failed since unzip can't extract to the same path that contains the same name of archive, for example, extracting /home/martin/archive.zip to /home/martin/archive.zip (even though it's the folder name) would failed. And yeah you're right that it would modify the custom name, which I'll try to solve it after. Nov 13, 2018 at 13:27
  • That works perfectly now! Btw I nerver see one uses ${AN%.*} before (and it looks shorter and more efficient than sed or awk, would you mind explain what the specifier did? Nov 13, 2018 at 14:15
  • 1
    I took it from here: stackoverflow.com/q/965053/850848#965072 - Explained here: gnu.org/software/bash/manual/html_node/… (though I cannot really say, I understand it fully). Nov 13, 2018 at 14:17

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.