Search code examples
batch-fileftpwinscp

Downloading non-existing files from a FTP server


I want to download only files that haven't been downloaded earlier from the FTP server. I am using WinSCP at the moment. I don't want to go to console and write the code, as I want to automate the process in the future. My current code is as follows :

"C:\Program Files (x86)\WinSCP\WinSCP.com"/command ^
     "open ftp://rnan:[email protected]/kgptel/" ^

     "lcd \D C:\Users\rnan\Desktop\Batch Files" ^
     "get -latest *" ^
     "exit"

This code just opens a session in console.

Please suggest changes that makes the files transfer automatically without me going to console and typing the code manually.

Thanks.


Solution

  • The primary problem is the empty line. You have to remove it, as it terminates WinSCP argument list (Or, if you want the new line for readability, you have to escape the new line with ^).

    You have other problems though:

    • The WinSCP lcd command accepts a single argument only. If you wanted to do, what Windows cd /D does, there's no need for any switch. WinSCP lcd switches the drive on its own.
    • You have to quote the argument of lcd as it contains spaces.
    • There should be space before the /command (while it works without the space too)
    "C:\Program Files (x86)\WinSCP\WinSCP.com" /command ^
        "open ftp://rnan:[email protected]/kgptel/" ^
        "lcd ""C:\Users\rnan\Desktop\Batch Files""" ^
        "get -latest *" ^
        "exit"
    

    See also Why are some WinSCP scripting commands specified in a batch file not executed/failing?


    As I've suggested you already, WinSCP GUI can generate a batch file template for you.

    Generate download batch file

    The only thing missing is the -latest switch.