# 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

# 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.

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

# 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.

See more: ,

### We've 3 Responses

1. 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.

2. how to get logs during SFTP ?

• Hi
You can add, log option when you call winscp, example:
“C:\Program Files (x86)\WinSCP\winscp.com” /log=D:\log\file.log /script=D:\scripts\sftpscript.txt”
Save it on a .bat file.