Installation, updates, general problem solving and assistance.
User avatar
!
25%
Posts: 2539
Joined: 2013-02-25 18:36

2015-06-27 02:49 »

Mirroring files to|from FTP (upload|download) with a batch file from the command-line.

These are very useful command-line files if you need to do backups or synchronize files and folders between FTP servers on a regular schedule without user interaction. Some sort of "robocopy.exe /MIR" for an FTP server. (I'm using WinSCP. I have also included the current release of WinSCP 5.7.4. Why use WinSCP? Well, because I didn't want to write a full FTP client and WinSCP is an excellent tool for this.)

Upload:

The special thing about "upload.bat" is that it has a "/delete" option which will delete each file from the local source directory after it has been uploaded. It first takes a list of the files before starting the upload process. For example, some systems need to talk with each other via flat "database" files (.XML/.CSV/.TXT etc.) and this will make sure that if you add a new file in the source directory during the upload process, the new file won't be deleted while the batch is running because there is no "bulk delete". The new file will be handled on your next scheduled run. This will always upload and overwrite files on the destination without any checks.
upload.png
upload.png (12.19 KiB) Viewed 1215 times
Mirror upload:

Mirrors and uploads from a local directory to a directory on the FTP server. It will do two rounds of mirroring and compares twice. Round one with modification time and round two with file size. This is to make sure that you don't have unfinished files in the destination directory, in case of, for example a connection loss during a scheduled run.
mirrordownload.png
mirrordownload.png (11.41 KiB) Viewed 1215 times
Download:

The special thing about "download.bat" is that it has a "/delete" option which will delete each file from the remote source directory after it has been downloaded. It first takes a list of the files on the FTP directory before starting the download process. For example, some systems need to talk with each other via flat "database" files (.XML/.CSV/.TXT etc.) and this will make sure that if you add a new file in the source directory during the download process, the new file won't be deleted while the batch is running because there is no "bulk delete". The new file will be handled on your next scheduled run. This will always download and overwrite files on the destination without any checks.
download.png
download.png (12.13 KiB) Viewed 1215 times
Mirror download:

Mirrors and downloads from directory on the FTP server to a local directory. It will do two rounds of mirroring and compares twice. Round one with modification time and round two with file size. This is to make sure that you don't have unfinished files in the destination directory, in case of, for example a connection loss during a scheduled run.
mirrordownload.png
mirrordownload.png (11.41 KiB) Viewed 1215 times
__________________________________________________________________________________________
(As always, there is no guarantee whatsoever. Use at your own risk.)

Version 1.4:
mirror.v.1.4.zip
(7.34 MiB) Downloaded 52 times
Changelog (2018-01-01):
  • Contains updated WinSCP 5.11.3.
__________________________________________________________________________________________
Older versions:

Version 1.3:
mirror.v.1.3.zip
(4.8 MiB) Downloaded 158 times
Changelog (2015-07-11):
  • The package is now called mirror<version>.zip instead of mirrorupload<version>.zip.
  • Added "download.bat" and "mirrordownload.bat".
Version 1.2:
mirrorupload.v.1.2.zip
(4.78 MiB) Downloaded 149 times
Changelog (2015-06-30):
  • Can now accept directories with spaces in the names.
  • Automatically creates destination directory if it doesn't exist.
Version 1.1:
mirrorupload.v1.1.zip
(4.77 MiB) Downloaded 149 times
Changelog (2015-06-30):
  • Longer reconnect time from 60 to default 120 seconds.
  • Compares twice, round one with modification time and round two with file size.
Version 1.0:
mirrorupload.v1.0.zip
(4.77 MiB) Downloaded 133 times
Changelog (2015-06-27):
  • Birth!
Attachments
mirrorupload.png
mirrorupload.png (11.27 KiB) Viewed 1215 times

User avatar
!
25%
Posts: 2539
Joined: 2013-02-25 18:36

2015-06-30 01:42 »

Some command-line examples:

  • mirrorupload.bat "ftp.mydomain.com" "ftpuser" "ftppassword" "c:myfiles" "/ftpuser/"
  • mirrorupload.bat "ftp.mydomain.com" "ftpuser" "ftppassword" "c:myfiles" "/ftpuser/" /delete
  • upload.bat "ftp.mydomain.com" "ftpuser" "ftppassword" "c:myfiles" "/ftpuser/" "*.*"
  • upload.bat "ftp.mydomain.com" "ftpuser" "ftppassword" "c:myfiles" "/" "*.txt"
  • upload.bat "ftp.mydomain.com" "ftpuser" "ftppassword" "c:myfiles" "/" "*.*" /delete

User avatar
!
25%
Posts: 2539
Joined: 2013-02-25 18:36

2015-06-30 01:54 »

*updated v1.1* :mrgreen:

User avatar
!
25%
Posts: 2539
Joined: 2013-02-25 18:36

2015-06-30 05:12 »

*updated v1.2* :mrgreen:

User avatar
!
25%
Posts: 2539
Joined: 2013-02-25 18:36

2015-07-11 02:45 »

*updated v1.3* :mrgreen:

User avatar
!
25%
Posts: 2539
Joined: 2013-02-25 18:36

2018-01-01 20:57 »

*updated v1.4* :mrgreen:

Post Reply