Getting "The filename, directory name, or volume label syntax is incorrect" when executing WinSCP from batch file for SFTP transfer

Viewed 69
@ECHO OFF
SETLOCAL
set /a count=5
set "filelist="
echo hello1
for /f "delims=" %%a in ('dir /s /r /b /a-d /o:-d "C:\09\Batch Scripting" ') do if defined count set "filename=%%a"  &call :transfer 
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
/command ^
"open sftp://username:password@X.X.X.X/ -hostkey=""ssh-rsa 1024 blah blah=""" ^
    "put %filelist% /users/sftpuser/ " ^
    "exit"
pause
goto :eof

:transfer
echo %filename%
set "filelist=%filelist% "%filename%""
set /a count-=1
if %count%==0 set "count="
goto :eof

The batch file successfully authenticates the connection, but later is fails with message:

The filename, directory name, or volume label syntax is incorrect

1 Answers

The batch file lines after line continuation character (^) must be indented:

"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
    /command ^
    "open sftp://username:password@X.X.X.X/ -hostkey=""ssh-rsa 1024 blah blah=""" ^
    "put %filelist% /users/sftpuser/ " ^
    "exit"

See also https://winscp.net/eng/docs/faq_batch_file#newline_escaping

Related