Getting '"..."' is not recognized as an internal or external command, operable program or batch file." when executing WinSCP commands from batch file

Viewed 39

I am trying to use a batch file to copy and replace a file from a SFTP site to a local C:\ using a batch file and Task Scheduler. This is being run on Windows Data Center 2019:

enter image description here

@echo off
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
  /log="C:\Users\xxxxxXxxxxxxxx\xxxxxxxxx\xxxxxxxxxx" /ini=nul ^
  /command
    "open sftp://xxxxxxxx/xxxxxxxx/ -hostkey=""xxxx""" ^
    "cd /foldercontainingfiletobecopied" ^
    "copy /thefolder/thetxttobecopied C:\destinationfolder\*" ^
    "exit"

set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
  echo Success
) else (
  echo Error
)

exit /b %WINSCP_RESULT%

After running the batch file, I get the following error:

Unknown command 'copy'
'"exit"' is not recognized as an internal or external command, operable program or batch file.

Any ideas of how to fix this issue or a better way to copy and replace the file? I have tried the following with no success:

  • xcopy
  • get/put
  • cp
1 Answers

Hard to tell without seeing the actual code with all whitespaces (as you have posted the code as an image).

You have probably multiple problems though:

  1. Most likely your batch file syntax for breaking long command into multiple lines is incorrect.

    The ^ must be very last character on the line, no spaces after it.

  2. WinSCP does not have copy command (nor xcopy). To download a file with WinSCP, use get

  3. There should be no ^ at the end of the command (after the "exit"). Though it should do no harm as long a the next line is empty.

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

Related