Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

TuttoTotto

%ERRORLEVEL% is always 0

Hey, great, it actually works that way. So the problem is really the same as with a for loop...
Many thanks for your support.
martin

Re: %ERRORLEVEL% is always 0

I do not think this is WinSCP problem. It's batch file problem.
See Errorlevel in a For loop (Windows batch)
(I know you are not using for, but I believe the issue is the same with if)
TuttoTotto

%ERRORLEVEL% is always 0

We use WinSCP.com (WinSCP-Portable V5.21.7) on a Windows Server 2019 Standard (64-bit OS, x64-based processor).

We have a batch file that calls WinSCP.com to upload specific files, if any, to an FTP server. That works so far.
Now we wanted to extend the batch file to send an e-mail if a problem with the upload pops up (e.g. connection problems...).
However, we found that %ERRORLEVEL% is always 0, regardless of whether the connection to FTP was successful or not.

Here is an excerpt from the batch file that calls WinSCP.com:
rem *** Set drive and directory for batch script execution...
d:
cd %HOME_DIR%
 
echo. >> %LOG_FILE%
echo ############################################# JOB START ############################################### >> %LOG_FILE%
echo %nowDATE% %nowTIME% >> %LOG_FILE%
echo ####################################################################################################### >> %LOG_FILE%
 
if exist %SEARCH_FILES% (
   echo ******************************************************************************************************* >> %LOG_FILE%
   echo Local files to upload found! Do upload to FTP server... >> %LOG_FILE%
   echo ******************************************************************************************************* >> %LOG_FILE%
   C:\Install\WinSCP-Portable\5.21.7\WinSCP.com /log=WinSCP_Log.log /ini=nul /script=FTP_ConnectionTest_Error.txt
 
   echo ERRORLEVEL %ERRORLEVEL% >> %LOG_FILE%
 
   if %ERRORLEVEL% equ 0 (
      echo ******************************************************************************************************* >> %LOG_FILE%
      echo Move local files to the backup directory..." >> %LOG_FILE%
      echo ******************************************************************************************************* >> %LOG_FILE%
      move /Y %HOME_DIR%\%SEARCH_FILES% %BACKUP_DIR% >> %LOG_FILE% 2>&1
 
   ) else goto JOB_ERROR
 
) else (
     echo ------------------------------------------------------------------------------------------------------- >> %LOG_FILE%
     echo No local files to upload! >> %LOG_FILE%
     echo ------------------------------------------------------------------------------------------------------- >> %LOG_FILE%
        )
 
goto JOB_END
 
:JOB_ERROR
echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx >> %LOG_FILE%
echo Error occurred! The job was canceled! Inform responsible users... >> %LOG_FILE%
echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx >> %LOG_FILE%
Powershell.exe -executionpolicy remotesigned -File SendWarningEMail.ps1
 
:JOB_END
echo ############################################# PROGRAM END ############################################# >> %LOG_FILE%
echo
echo off

The script file contains nothing magical for our tests because we expect a connection problem anyway:
open ftp://toto:tutu@egFTP.com/
option transfer binary
dir *
exit

The log-file of the batch always shows 0 for %ERRORLEVEL% and moves the target file to the backup directory even if a connection to the FTP could not be established and we actually expect something other than 0 for %ERRORLEVEL%. Here what we see in this log file:
############################################# JOB START ############################################### 

14.02.2023 14:26:44.46
#######################################################################################################
*******************************************************************************************************
Local files to upload found! Do upload to FTP server...
*******************************************************************************************************
ERRORLEVEL 0
*******************************************************************************************************
*******************************************************************************************************
        1 file(s) moved.
############################################# PROGRAM END #############################################

And that is from the WinSCP log file:
...
. 2023-02-14 14:26:44.713 --------------------------------------------------------------------------
< 2023-02-14 14:26:44.729 Script: Connecting to egFTP.com ...
. 2023-02-14 14:26:44.729 Connecting to egFTP.com ...
. 2023-02-14 14:26:54.756 Connection failed.
< 2023-02-14 14:26:54.756 Script: Connection failed.
< 2023-02-14 14:26:54.756 Connection failed.

Why do we always get 0 in %ERRORLEVEL%, even if connection fails? What are we missing?
Thanks for your support