Error Code always 0

Advertisement

bobrico62
Joined:
Posts:
3
Location:
USA

Error Code always 0

I have a simple script that transfers files no issues but success or failure it shows as success. Would appreciate any guidance.

The batch file:
@echo off
 
"D:\temp\WinSCP\WinSCP.com" ^
  /log="D:\temp\winscp\WinSCP.log" /ini=nul ^
  /command ^
    "open sftp://CM111XXXSI:XxXXxXXX@QAFILEXMIT:10088/ -hostkey=""ssh-rsa 2048 *****************************************"" -privatekey=""D:\certificate\e*********.ppk"" -passphrase=""***********"" -rawsettings AuthKI=0 GSSAPIFwdTGT=1 KEX=""rsa,ecdh,dh-gex-sha1,dh-group14-sha1,WARN,dh-group1-sha1""" ^
    "put -nopreservetime -nopermissions D:\aews\maintenance.application\export\*.fil" ^
    "ECHO ERRORLEVEL %ERRORLEVEL%" ^
    "exit"
 
set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
  echo Success
) else (
  echo Error
)
 
exit /b %WINSCP_RESULT%

Here's the output with 0 files in the local dir:
d:\escrow\jobs>filetransfer
Searching for host...
Connecting to host...
Authenticating...
Using username "CM111XXXSI".
Authenticating with public key "*******".
Further authentication required
Authenticating with pre-entered password.
Authenticated.
Starting the session...
Session started.
Active session: [1] CM111XXXSI@*********
No file matching '*.fil' found.
ERRORLEVEL 0
Success

Reply with quote

Advertisement

bobrico62

Hi Martin, any idea what I am doing wrong?

I have tried batch file method and a native WinSCP script with no luck. Whenever files are not in the source DIR and transfers fail,, it still comes back as 0 (success)

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,034
Location:
Prague, Czechia

Re: Error Code always 0

You need:
    "option failonnomatch on" ^
    "put -nopreservetime -nopermissions D:\aews\maintenance.application\export\*.fil" ^
See https://winscp.net/eng/docs/scriptcommand_option#failonnomatch

Other thing is that the %ERRORLEVEL% in your WinSCP commandline will be expanded by Windows command interpreter before it even runs WinSCP. You you will always get 0 there.
To get the actual value, you need to print the value after WinSCP finishes:
set WINSCP_RESULT=%ERRORLEVEL%
ECHO ERRORLEVEL %WINSCP_RESULT%

Reply with quote

Advertisement

You can post new topics in this forum