Software Developer

WinSCP Script To Upload/Sync Local Directory To Server

This WinSCP script will automatically upload all files from your local folder up to your server, while deleting any files in the server’s directory ( remote directory ) that do not exist in your local folder. In other words, it will sync your server with what is in your local directory.

This is useful if you use Notepad++ with the NppFTP plugin to publish your website’s files to your server. This is a quicker way to sync your server with what is in your local folder, since the NppFTP only seems to let you upload one file at a time.

The following script goes into a regular text file. Name it whatever you like, but give it the .txt extension.

Before using this script, please note, again, this script will make the folder on the server match your local folder exactly — it deletes any files in the server’s folder that are not in your local folder. If you want to only upload/sync your existing local files without deleting any files from the server, then remove the “-delete” from the lines below that begin with “synchronize remote -delete“.

# Automatically abort script on errors
option batch abort

# Disable overwrite confirmations that conflict with the previous
option confirm off

# Force binary mode transfer
option transfer binary

open sftp://FTP_USERNAME:FTP_PASSWORD@FTP_DOMAIN.COM/ -hostkey="ssh-dss 1024 ##:##:##:##:##:##:##:##:##:##:##:##:##:##:##:##"

# Publish local theme up to server, while deleting obsolete files from server.

synchronize remote -delete C:\local\path\to\your\site\folder /home/content/remote/path/to/your/live/site/on/server/

echo Job is complete.

exit

Make the following changes to the script.

  1. Change “FTP_USERNAME” on line 10 to your own FTP username. Be careful to leave the colon (:) in place.
  2. Change “FTP_PASSWORD” on line 10 to your own FTP password. Be careful to leave the @ symbol in place.
  3. Change “FTP_DOMAIN.COM” on line 10 to your own FTP domain.
  4. Change “ssh-dss 1024 ##:##:##:##:##:##:##:##:##:##:##:##:##:##:##:##” on line 10 to your host key. (To obtain your host key, you’ll have to first connect to your server using the regular WinSCP console, then click on “Commands -> Server/protocol Information”, then see the “Server Host key Fingerprint” box. Copy your host key from there and paste it into this script. Then you can log out of WinSCP.)
  5. On line 14, change C:\local\path\to\your\site\folder to the location of your local folder on your computer.
  6. On line 14, change /home/content/remote/path/to/your/live/site/on/server/ to the path of your remote folder on your server.

Add More

You can sync more than one folder in the script. For example, the following script syncs 4 different folders:

# Automatically abort script on errors
option batch abort

# Disable overwrite confirmations that conflict with the previous
option confirm off

# Force binary mode transfer
option transfer binary

open sftp://FTP_USERNAME:FTP_PASSWORD@FTP_DOMAIN.COM/ -hostkey="ssh-dss 1024 ##:##:##:##:##:##:##:##:##:##:##:##:##:##:##:##"

# Publish local theme number 1 up to server, while deleting obsolete files from server.

synchronize remote -delete C:\local\path\to\your\site\folder /home/content/remote/path/to/your/live/site/on/server/

# Publish local theme number 2 up to server, while deleting obsolete files from server.

synchronize remote -delete C:\local\path\to\your\site\folder\number\2 /home/content/remote/path/to/your/live/site/number/2/

# Publish local theme number 3 up to server, while deleting obsolete files from server.

synchronize remote -delete C:\local\path\to\your\site\folder\number\3 /home/content/remote/path/to/your/live/site/number/3/

# Publish local theme number 4 up to server, while deleting obsolete files from server.

synchronize remote -delete C:\local\path\to\your\site\folder\number\4 /home/content/remote/path/to/your/live/site/number/4/

echo Job is complete.

exit

To run the script, see How To Run a WinSCP script from Windows Command Line.

By

We've 2 Responses

  1. July 28th, 2015 at 8:49 pm

    Dear Isobel:
    Thanks greatly for helping me to avoid a drill the skull with knowledge session by providing this great batch job example for WINSCP.

    Best regards.

    avatar

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]