Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

bladeoflight16

I'll also add that if you're not keen on modifying mkdir, creating a completely separate command with this behavior would also be fine. I'm not too particular about the implementation as long as the functionality is available somehow (other than suppressing all errors).
bladeoflight16

As my first post said,
Another option is to switch to writing code in a separate language interface, but that's an incredibly heavy handed solution for such a simple and common need.

This is a very reasonable request. Ensuring a folder exists before you proceed with further commands is an extremely common need, and adding an option to support it is a very modest request.

If you're not going to put in minimal features to make the scripts applicable to the common use cases and you're just going to point everyone to .NET COM interfaces (which are undesirable for introducing a native dependency to .NET code if for no other reason), what's the point of having a scripting interface at all?
bladeoflight16

Yes, you can, but that isn't really an improvement. The cd in my code above will also error out if it doesn't get created. But this is still a cumbersome work around and hides the location of the actual problem. Delete if exists and create if not exists operations are a very common pattern throughout software because these sorts of needs are so common, and having them built in greatly simplifies those situations. (Although delete if exists is generally less relevant to WinSCP, since files will much more often be created than deleted.)

The real problem is that error hiding to work around a missing feature is bad form/practice. This isn't me catching an error to do something with it; this is me silently swallowing and error and continuing. Adding this very modest feature would allow script writers to avoid doing so.
martin

Re: Add option to mkdir to help with directory already existing

You can use stat to make sure the folder exists after mkdir:

option batch continue
mkdir /my/remote/dir
option batch abort
stat /my/remote/dir
bladeoflight16

Add option to mkdir to help with directory already existing

A common need when scripting is to create a directory if it does not exist and to proceed if it already does. However, the scripting interface does not contain a way to check if it exists or to skip creation if it does.

The best work around at the moment appears to be disabling errors before attempting to create the directory:

option batch continue

mkdir /my/remote/dir
option batch abort
cd /my/remote/dir
put ./myfile.txt


But this can also cause the script to continue in other error cases, such as a failure due to not having permissions. Another option is to switch to writing code in a separate language interface, but that's an incredibly heavy handed solution for such a simple and common need.

It would be better if mkdir supported continuing when the directory exists out of the box. Something like this, for example:

mkdir -skipifexists /my/remote/dir

cd /my/remote/dir
put ./myfile.txt


(The option name could be something different, of course.)

Here are some threads referencing this need:

https://winscp.net/forum/viewtopic.php?t=15867
https://winscp.net/forum/viewtopic.php?t=8336
https://winscp.net/forum/viewtopic.php?t=4580