Issues catching return code and %ERRORLEVEL%

Advertisement

Prostagma
Guest

Issues catching return code and %ERRORLEVEL%

Hi Martin and all,

I got myself into trouble trying to catch the return code from winscp.com (latest version 5.18.5).

My script works fine but when I'm trying to make the winscp part fail (for testing purposes, @IP is voluntarily a false one), it indeed fails (connection fails) but still returning %ERRORLEVEL%=0

My script looks like this:
"%repdev%\WinSCP.com" ^
/log="%replog%\FTP_%soc%_%tagdate%.log" /loglevel=2* /logsize=10*10M ^
/command ^
       "option batch abort" ^
       "open ftp://root:XXXXXXX@120.1.1.1/" ^
       "cd /ud/tmp/LBE" ^
       "put -transfer=binary PDF%soc%.001" ^
       "put -transfer=binary PDF%soc%.flw" ^
       "exit"
if %ERRORLEVEL% NEQ 0 GOTO :ERREUR_FTP
I'm guessing it's because winscp itself returns a 0, because when i'm looking at the log, there is no "Script : Exit code: X" as you can see.

I tried the same using /script with a txt instead of /command but the result is the same.

Any ideas what's wrong with my script? Or am I misunderstanding the way WinSCP deals with failed connection?

Thanks.

err.jpg

Reply with quote

Advertisement

Guest

Re: Issues catching return code and %ERRORLEVEL%

Sure then, here it is.
  • FTP_4SAISONS_20210602.log (4.31 KB, Private file)

Reply with quote

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

Re: Issues catching return code and %ERRORLEVEL%

Indeed, it seems that the exit code is not logged in this scenario. I'll look into it.

Though the exit code is returned correctly for me. If I test your batch file, I get:
batch           abort     
Connecting to 120.1.1.1 ...
Connection failed.
No connection could be made because the target machine actively refused it.

Connection failed.
Exit code: 1
Max roundtrip: 0
The system cannot find the batch label specified - ERREUR_FTP
So the if is evaluated correctly.
What output do you get?

Reply with quote

Prostagma
Guest

I got the Exit code: 1 in my output, though the if seems to be evaluated with a ERRORLEVEL=0 cause it doesn't enter the ERREUR_FTP subroutine.
batch           abort     
Connecting to 120.1.1.1 ...
Connection failed.
Timeout detected. (control connection)

Connection failed.
Exit code: 1
Max roundtrip: 16
Commande ECHO d‚sactiv‚e.
You can see the complete output in attached file.

I even changed my code to force the script to enter my ERREUR_FTP subroutine and it worked, confirming that %ERRORLEVEL%=0
if %ERRORLEVEL% EQU 0 GOTO :ERREUR_FTP
and here is the output obtained:
batch           abort     
Connecting to 120.1.1.1 ...
Connection failed.
Timeout detected. (control connection)

Connection failed.
Exit code: 1
Max roundtrip: 0
generating this log (sorry for the French) showing it's entering the :ERREUR_FTP subroutine
ENVOI FICHIER SFTP 4SAISONS 
Probleme lors de l'envoi FTP 
le programme ne s'est pas terminé correctement
  • pouet.txt (3.4 KB, Private file)

Reply with quote

Advertisement

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

Can you start with a trivial (and exact) example like this one:
@echo off
set repdev=C:\path\to\winscp
"%repdev%\WinSCP.com" /log="%TEMP%\dummy.log" /loglevel=2 /command "exit"
echo %ERRORLEVEL%
"%repdev%\WinSCP.com" /log="%TEMP%\dummy.log" /loglevel=2 /command "error"
echo %ERRORLEVEL%
I get this:
Exit code: 0
Max roundtrip: 0
0
Unknown command 'error'.
Exit code: 1
Max roundtrip: 0
1

Reply with quote

Prostagma
Guest

With that simple script, using my own directories in the %repdev% and %TEMP% variables, I get the same output :
Exit code: 0
Max roundtrip: 0
0
Unknown command 'error'.
Exit code: 1
Max roundtrip: 0
1

Reply with quote

Prostagma
Joined:
Posts:
1
Location:
France

I made some other tests with and without using the caret symbol, modifying the
-command
to make it simple as possible like this :
"%repdev%\WinSCP.com" /log="%replog%\FTP_%soc%_%tagdate%.log" /loglevel=2 /command "open ftp://root:XXXXXXX@120.1.1.1/"
Next line of the script is only
echo %ERRORLEVEL%

And I get this output :
Connecting to 120.1.1.1 ...
Connection failed.
Timeout detected. (control connection)
Connection failed.
Exit code: 1
Max roundtrip: 16
0

Reply with quote

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

With your example I get:
Connecting to 120.1.1.1 ...
Connection failed.
No connection could be made because the target machine actively refused it.
Connection failed.
Exit code: 1
Max roundtrip: 16
1
I have sent you an email with a debug version of WinSCP to the address you have used to register on this forum.

Reply with quote

Advertisement

You can post new topics in this forum