miércoles, 15 de octubre de 2014

Sync a local folder with remote folder FTP a combination of Robocopy and WinSCP

Here is the situation:

I need to update a FTP server with the latest documents from our client's server. Latest documents included New and Modified Documents.

I will be using 2 tools, both free.
1.- Robocopy, it is free and it is available on Win 7 and up, just open command prompt window and type Robocopy, press enter if you see this screen you have it, otherwise you can download it from the web.
2.- WINSCP this is a great Free FTP client that allows you to do Scripts. you can download it from: http://winscp.net/eng/download.php

before I do the steps let me explain the options i will be using from these 2 tools.
A.- Robocopy has an OPTION

  • /S : means include sub directories
  • /R:n: Means retry to copy the files N times, if you use 0 then it will skip that file
  • /MAXAGE:NDays: means exclude all the files older than Ndays or you can put a date.


B: WinSCP I will be using the Synchronize option

so now let do the steps:

I. Copy all the Newest Files from the Source Folder to a Temp Destination folder, to accomplish this I create the following DOS script:


rd d:\uploadimages /s /q
md d:\uploadimages

robocopy D:\Images d:\uploadimages /s /r:0 /MAXAGE:10

It is pretty simple, removing the temp folder I am using, "d:\uploadimages"
recreating it again and copying all New/Modified documents from the last 10 days, from my source "d:\images" to my Temp folder "d:\uploadimages"

II. Create the script to upload this temp folder to my FTP

First you need to define your FTP server in Winscp, follow this link http://winscp.net/eng/docs/ui_login


Test it and be sure you can connect.


Second. open notepad and type the following:

option batch on
option confirm off
open FTPSERVER
synchronize remote "d:\uploadimages" "/Client/Uploadimages"
close

exit


And save it as d:\upload.txt or any name, FTPSERVER needs to match the sever name in WinSCP.
what are we doing here is pretty simple, open a connection to the FTP server and synchronize the content of the Local folder to the Remote Folder, simple right?

now to execute this script you call it like this from Command Prompt:
cd "C:\Program Files (x86)\WinSCP\"
winscp.com /script=d:\upload.txt

you will see something like this:

and the program will compare and upload the new/modified files.

putting these 2 steps together you end with something like this:

------begin-----
rd d:\uploadimages /s /q
md d:\uploadimages
robocopy D:\Images d:\uploadimages /s /r:0 /MAXAGE:10
cd "C:\Program Files (x86)\WinSCP\"

winscp.com /script=upload.txt
------end-----
save it as a Batch file and add it to the schedule task, test it and make sure it works.

I hope this helps you.


Sources:
http://winscp.net/eng/docs/task_synchronize_full#automating_synchronization
http://winscp.net/eng/docs/ui_login
http://technet.microsoft.com/en-us/library/cc733145.aspx

No hay comentarios.:

Publicar un comentario