Moving files to FTP in batch file and delete source files

7,268

robocopy and xcopy work with local files only.


With ftp, you can do the upload and then delete source files with local del command.

ftp -s:upload.txt
del *.*

But it's difficult to implement this in a way, that only files that were really successfully uploaded are deleted. The above batch file will delete all files, no matter, if some of them failed to upload, or if some of them were created between the ftp and del.


But most 3rd party FTP command-line clients can do this easily. For example, with WinSCP FTP client, you can simply use the following batch file (using put command with the -delete switch):

winscp.com /ini=nul /log=upload.log /command ^
    "open ftp://username:[email protected]/" ^
    "put -delete C:\local\source\path\* /remote/target/path/" ^
    "exit"

Regarding scheduling: See WinSCP guide to scheduling transfers to FTP server.

(I'm the author of WinSCP)

Share:
7,268

Related videos on Youtube

David
Author by

David

Updated on September 18, 2022

Comments

  • David
    David over 1 year

    I need to create batch script for copy files from PC to FTP server. After copy - delete all files from source. And in destination directory overwrite all existing files.

    I found many solutions, but I am not sure which one is best for me. For example XCOPY or ROBOCOPY or classic method with commands like ftp, cd, copy, mget, ...

    ROBOCOPY seems like best option, but I think that it does not support FTP right. XCOPY is older method, ...

    After all it will be regular scheduled task...

    Thank you for your advices.