Search code examples
winscp

WinSCP won't execute the entire script specified on a command-line


My very first post but before coming here I did a research on the issues I have. Mostly on WinSCP forums but it came to no fruition.

Using winscp.com, I would like a script to use get command to download multiple files using SFTP. Here is my version:

echo ENTER WEEK NUMBER

set /p input=""

cls

Rem Create folder based on date

set year=%date:~6,4%


mkdir \\11.111.111.11\Folder1\"Folder 2"\#Folder3\%year%_WK%input%

mkdir \\11.111.111.11\Folder1\"Folder 2"\#Folder3\%year%_WK%input%\User_1

mkdir \\11.111.111.11\Folder1\"Folder 2"\#Folder3\%year%_WK%input%\User_2

mkdir \\11.111.111.11\Folder1\"Folder 2"\#Folder3\%year%_WK%input%\User_3

c:

cd Program Files (x86)

cd Winscp

WinSCP.com /command ^

        "#echo off" ^

        "# Connect to server"^

        "Open sftp://Uname:[email protected]" ^

        "# batch off mode" ^

        "option batch off" ^

        "lcd \\11.111.111.11\Folder1\""Folder 2""\#BFolder3\%year%_WK%input%\User_3" ^

        "cd .."^

        "cd RemoteFolder/USer_Folder" ^

        "get %year%WK%input%.zip"^

        "cd .."^ 

        "cd User_2_Folder" ^

        "get User2WK%input%.csv"^

        "get SAV_LACWK%input%.csv"^

Issue is, the WinSCP executes cd .. after the first get command and it just stops. It won't go any further:

enter image description here

If I copy/paste at the command prompt, it works fine.

Any idea?

Thank you Donne


Solution

    1. All your absolute local paths use a wrong syntax C\: for a drive. The correct syntax is C:\.

      mkdir C\:%year%Folder\
      mkdir C\:%year%Folder\Data_User
      ...
      
      WinSCP.com /command ^
          ....
          "lcd C\:%year%Folder\Data_User" ^
      
    2. You are missing a quote and an escape character (^) after

          “# Change remote directory
      
    3. You are using fancy quotes ( and ), instead of the plain double-quotes (") in:

          “#Set local directory”^
          ....
          “# Change remote directory
      
    4. You have a space after the ^ in

      "cd .."^ 
      

      This effectively makes the ^ be ignored (it escapes the space, not the newline).

    See WinSCP FAQ Why are some scripting commands specified on WinSCP command-line in a batch file not executed/failing?


    If this does not help, we need to see the output of the batch file run (ideally with removed @echo off).