This is an old revision of the document!

Scripting/Automation

See Command-line parameters to learn how to enter the console/scripting mode.

Advertisement

Commands

Paul is great The following commands are implemented: call, cd, chmod, close, exit, get, help, keepuptodate, lcd, lls, ln, lpwd, ls, mkdir, mv, open, option, put, pwd, rm, rmdir, session, synchronize and several aliases.

The command help <command> shows a comprehensive description of the command including examples and aliases.

Multiple sessions can be opened simultaneously. Use the session command to switch between them. Commands can be read from a script file or passed from the command-line using the /command switch.

By default interactive mode is used (the user is prompted in the same way as in GUI mode). To switch to batch mode (all prompts are automatically answered negatively) use the command option batch on. For the batch mode it is recommended to turn off confirmations using option confirm off to allow overwrites (otherwise the overwrite confirmation prompt would be answered negatively, making overwrites impossible).

Note also that the first connection to an SSH server requires verification of the host key, which cannot be automated by scripting. Therefore, some provision for accepting keys must be made prior to running the script to connect to a new host. Never attempt to make the script verify the host key automatically.

WinSCP returns exit code 1, when any command is interrupted due to an error or any prompt is answered Abort (even automatically in batch mode). Otherwise it returns the exit code 0.

The Console Interface Tool

As WinSCP3.exe is a GUI application, it cannot inherit the console window when run from another console application (such as the Windows command-prompt). To allow this, run WinSCP using the console interface tool WinSCP3.com (you can find WinSCP3.com in the main installation package).

Advertisement

Running a Script under a Different Account

If you are going to run the script under a different account (for example using the Windows scheduler), note that WinSCP stores its configuration to the user part of Windows Registry by default. So you may need to either transfer the configuration from your account registry to the other account registry or use the INI file instead.

Note that the configuration also includes verified host keys, which you may need to transfer as well (or confirm manually under the account) to allow automatic execution of the script.

Example

The example below connects to example.com server with account user, downloads file and closes the session. Then it connects to the same server with the account user2 and uploads the file back.

# Automatically answer all prompts negatively not to stall
# the script on errors
option batch on
# Disable overwrite confirmations that conflict with the previous
option confirm off
# Connect using a password
# open user:password@example.com
# Connect
open user@example.com
# Change remote directory
cd /home/user
# Force binary mode transfer
option transfer binary
# Download file to the local directory d:\
get examplefile.txt d:\
# Disconnect
close
# Connect as a different user
open user2@example.com
# Change the remote directory
cd /home/user2
# Upload the file to current working directory
put d:\examplefile.txt 
# Disconnect
close
# Exit WinSCP
exit

Save the script to the file example.txt. To execute the script file use following command. As the script connects the session itself, using the command open, omit the session command-line parameter.

winscp3.exe /console /script=example.txt

Advertisement

Instead of using open command you can also open a session using a command-line parameter.

winscp3.exe /console /script=example.txt user@example.com

Useful Scripts

You can see list of scripts other users found useful.

Last modified: by 24.62.33.32