WinSCP is a great Windows GUI utility to copy files to and from Linux boxes, but many don’t know about the powerful WinSCP commandline functionality that are extremely useful to setup jobs to periodically copy files from Windows over to Linux or vice versa. All it takes is the correct syntax to call the executable and then switches to perform the actions you want to perform.
Recently, in working with a client who wanted to feed a web service a CSV file from a directory on a Windows box, they ran into issues trying to get the service to read the remote file. However, in suggesting using WinSCP to first copy the file over to the Linux box and then have the web service read the current file locally, this resolved the issue they were seeing.
Let’s take a look at the syntax required to call the WinSCP program and tell it to SCP a file from Windows to Linux.
Syntax and switches
"c:program files (x86)WinSCPwinscp.com" /command "option confirm off" "open root:pas[email protected]" "put c:exportfile.csv /var/www/import/" "exit"
The command above first calles the winscp.com file from the program directory and feeds it the options we want. The option confirm off switch tells it that we don’t want it to make us confirm things like file overwrites and such as we want to use this with a scheduled task. Then we simply are pointing it to Linux server in question and telling it which local Windows file we want to send over to the web directory on the Linux server. The final exit command simply exits the winscp program and sends us back to the prompt which finishes our batch file.
The last order of business is to simply setup a scheduled task run the batch or exe that we create containing the scripted winscp.com.
- Create Basic Task
- Point it to the batch with the winscp.com script
- Set your schedule
- Make sure you have the options enabled to Run with highest privileges
- And the option Run whether user is logged on or not
The above options will make sure the script doesn’t run into system rights issues, as well as will make sure the program runs whether there is a user logon session running or not.
WinSCP although not really known as well for its commandline potential, is a great way to schedule file copies from Windows to Linux boxes or for other system admin tasks.