Skip to Content

Creating and Configuring WINSCP Scripts to Transfer Files from File System to SFTP

This guide will step you through how to create and configure WINSCP scripts to transfer files from a File System to SFTP. The expected time for this setup should take around 10-15 minutes. For background, this approach is being used for a client generating .xlsx files from Vinyl that a 3rd party company uses to determine employee access to reports in the client system. So this information gets generated out of Vinyl first, and then transferred over to the 3rd party server following the process outlined in this guide.

How to Configure

  1. Create a directory new directory on your Web Server and add two files to it — winscp.log and {{someName}}Export.txt
  2. In the {{SomeName}}Export.txt file, add the following code to be inline with your setup:

    open sftp://{{username}}:{{password}}@{{hostname}} -hostkey="ssh-rsa 2048 {{hostkey}}"
    cd /path/to/directory/on/sftp
    put -nopermissions -nopreservetime -delete C:\{{LocalPathToFile}}\*
    exit
    

    In the code block example, where it reads sftp://{{username}}:{{password}}@{{hostname}}, {{username}}, {{password}}, and {{hostname}} should be replaced with the SFTP login credentials and hostname, and the -hostkey refers to the server hostkey fingerprint which can be found in WinSCP > Session > Server and protocol information > Server host key fingerprint. See screenshot:

    Img 1

    cd /path/to/directory/on/sftp refers to the root directory in the target SFTP folder that the files should be moved to.

    put -nopermissions -nopreservetime -delete C:\{{LocalPathToFile}}\* refers to the location of the file on the source server (Vinyl server) that the file exists, while the * is a wildcard that tells the script to take any files within that directory.

    It should also be noted that the-deletein the script will delete your file as soon as it moves (or tries to move it). This flag can be removed if you would like to keep the file in your source directory.

  3. Open Windows Task Scheduler on the web server and create a new task to your specs. In this particular setup, the schedule is configured to run every 5 minutes, which is accomplished with the 'Repeat task every 5 minutes for a duration of 1 day' option, on a daily schedule:

    Img 2

  4. Double click on the task you just created in step 3. Under the Actions tab, create an action Start a program:

    • In Program/Script select the location of the WinSCP.exe
    • In Add arguments (optional) put in the following string /console /script="{{someName}}Export.txt" /log="winscp.log" where {{someName}}Export.txt is the name of your file created in step 1, and winscp.log is your winscp.log file also created in step 1.
    • In Start in (optional) add the "new directory" you created in step 1

    Img 3

Your task should now be setup to move files from the specified directory to the SFTP! You can run your task on demand from Windows Task Scheduler and check the winscp.log file if there are any errors.

WinSCP has some very useful documentation if you wish to extend the script or do something similar with WinSCP: https://winscp.net/eng/docs/scripting