I'm not an expert in Windows batch files, but apparently the
%ERRORLEVEL%
in a block wrapped in brackets is resolved before that block starts. So it never reflects WinSCP exit code.
If you remove the brackets, it works:
WinSCP /command "option batch continue" "option confirm off" "open HPAY_UAT" "lcd C:\Users\a160559\Desktop\DSC_TRANS\FILES\" "cd /data/dsc_files/" "put COMP_UPDATE.log /data/dsc_files/COMP_UPDATE.log" "stat /data/dsc_files/COMP_UPDATE.log" "exit"
if %ERRORLEVEL% neq 0 goto error
echo Error or file /data/dsc_files/COMP_UPDATE.log exists
exit 0
:error
echo Error or file /data/dsc_files/COMP_UPDATE.log not exists
exit 1
You can wrap your batch file to another batch file and do output redirection there.
Or if you need the brackets, use plain old
if errorlevel 1
:
(
WinSCP /command "option batch continue" "option confirm off" "open HPAY_UAT" "lcd C:\Users\a160559\Desktop\DSC_TRANS\FILES\" "cd /data/dsc_files/" "put COMP_UPDATE.log /data/dsc_files/COMP_UPDATE.log" "stat /data/dsc_files/COMP_UPDATE.log" "exit"
if errorlevel 1 goto error
echo Error or file /data/dsc_files/COMP_UPDATE.log exists
exit 0
:error
echo Error or file /data/dsc_files/COMP_UPDATE.log not exists
exit 1
) > C:\Users\a160559\Desktop\Stat_out_exec_1.log