Search code examples
batch-filesftpwinscp

Is there a way to reference a WinSCP script's directory to download files there?


I have a pretty standard WinSCP SFTP script that grabs a few files and saves them to a SharePoint library. The script gets called from VBA so I cannot leave lcd blank. The location of the library is slightly different for different users so I need a way to get the script's location.

The script is stored in a synced SharePoint folder: %USERPROFILE%\User-Path\FTP\

For other users that path might be %USERPROFILE%\UserPath-FTP\FTP.

@echo off

"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
 /command ^
    "open sftp://mysite.gov/ -hostkey=""ssh-rsa 4096 key#######=""" ^
    "cd /" ^
    "lcd ""%CD%""" ^
    "get File1.TXT" ^
    "get File2.csv" ^
    "get File3.csv" ^
    "exit"
timeout 10
exit

This code does not work when called from VBA, it fails to download the files to the lcd.


Solution

  • If I understand it correctly, you want WinSCP to download the files to the folder where the batch file is stored? (there's no WinSCP script involved)

    See Get current batchfile directory.

    You can use it like this:

    @echo off
    
    cd /D %~dp0
    
    "C:\Program Files (x86)\WinSCP\WinSCP.com" ^
     /command ^
        "open sftp://example.com/ -hostkey=""ssh-rsa 4096 key#######=""" ^
        "cd /" ^
        "get File1.TXT" ^
        "get File2.csv" ^
        "get File3.csv" ^
        "exit"
    

    (and drop the lcd line from WinSCP commandline)