batch-filewinscpnetwork-drivemapped-drive

WinSCP - Download file from SFTP server to network location, then delete


I need to write a WinSCP script or batch file that will download the contents of a remote SFTP folder, delete contents and then upload to a mapped network folder. I know how to write the login part of the script. But I am confused as to what the specific command line should look like. From what I've read the command script to download and delete the file is something like this:

get -delete -transfer=binary *

The part that I'm not getting is the upload to the network folder command. I understand put is the WinSCP upload command operator. But how do I use it in conjunction with the network folder location files should be uploaded to? Also, can this be scripted out in a batch file or do the batch file and script need to be written separately? Thanks!


Solution

  • An "upload" to a mapped network folder is not a job for WinSCP. A mapped network folder behaves as a local drive. A plain Windows copy command will do. So first do your WinSCP download and then copy (it's not really called an upload) the files to the network drive.

    winscp /ini=nul /command ^
        "open sftp://user:password@example.com/" ^
        "get -delete /sftp/path/* c:\local\drive\path\" ^
        "exit"
    
    copy c:\local\drive\path\* n:\mapped\network\drive\path\
    

    Though, if you do not need the files locally, you can have WinSCP download the files from the SFTP folder directly to the mapped network drive:

    winscp /ini=nul /command ^
        "open sftp://user:password@example.com/" ^
        "get -delete /sftp/path/* n:\mapped\network\drive\path\" ^
        "exit"