Search code examples
batch-filessissftpwinscp

How to rename a downloaded file from within the WinSCP script?


I have WinSCP script that downloads a file from the SFTP server to the local machine. How can I rename the file from within the WinSCP script?

The script is being run from Windows machine.

The WinSCP script:

option batch off 
option confirm off 
open sftp://username:password@servername -hostkey=""
option transfer binary
get -latest "oldname.csv" c:\localfolder 
mv c:\localfolder\oldname.csv latestname.csv
exit

I tried renaming using the commands:

rename InitialFileName FinalFileName

then tried using:

mv InitialFileName FinalFileName

But the script is throwing the error:

File not found, Language: en 

Tried stuff from this blog


Solution

  • If you want to download the file to a different name, specify the new name directly in the get command:

    get "oldname.csv" c:\localfolder\latestname.csv
    

    (Note that when downloading one specific file, the -latest switch is pointless. The latest file out of one file[s] is that one file.)