Not getting errorlevel 1 from exception

Advertisement

fflo92
Guest

Not getting errorlevel 1 from exception

Hi,

I generate the following script from external program:
open sftp://censored/
lcd \\censored\
cd /censored/IN
put *
close
exit
It is called with the following batch which is also generated from external program:
@echo off
\\censored\WinSCP\WinSCP.exe /log="\\censored\WinSCP\censored\log\WinSCP_DEV_!D_!M_!Y_!T.log" /ini=nul /script="\\censored\WinSCP\censored\scriptname.txt"
if ERRORLEVEL EQ 1 echo ERRORLEVEL>\\censored\WinSCP\censored\sftpError.txt
Both script and batch are working fine. To evaluate exception handling with the error file, I specified wrong server path on purpose in the WinSCP script. As expected, I get the following exception in my log:
(Exception) **Expected host key was not configured, use -hostkey switch.**
However, no error file is created. I've also used different approaches to try to create error file. As they have worked in the past, I am pretty sure it is not about my script but about how WinSCP throws the Errorlevel. It seems like WinSCP doesn't return Errorlevel=1. Can you tell me why is that?

Reply with quote

Advertisement

fflo92
Guest

I don't know why but extracting the line
ERRORLEVEL>\\censored\WinSCP\censored\sftpError.txt
into a separate line fixed the problem. Is the same if I use %ERRORLEVEL%

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
40,567
Location:
Prague, Czechia

Re: Not getting errorlevel 1 from exception

I do not think this has anything to do with WinSCP. It's your batch file syntax that is wrong. There's no if ERRORLEVEL EQ 1. It should be:
if %ERRORLEVEL% EQU 1
(though better is GEQ)
or
if ERRORLEVEL 1
See links in How do I know that script completed successfully?
and https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/if

Also you should use winscp.com and not winscp.exe.

Reply with quote

Advertisement

You can post new topics in this forum