%ERRORLEVEL% is always 0

Advertisement

Tina_ls
Guest

%ERRORLEVEL% is always 0

Hello
I have a problem.
I'm using the below script.
"F:\Tools\WinSCP_51710\WinSCP.com" ^
  /log="F:\Tools\WinSCP_51710\WinSCP_51710.log" /ini=nul ^
  /command ^
    "option batch abort" ^
    "open ftp://%ftp_username%:%ftp_password%@%FTP_IP% -explicit -certificate=*" ^
    "ls "%FTP_DIR_LIVE%"" ^
    "exit"
 
set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
  echo Success
) else (
  echo Error
)
ECHO ERRORLEVEL %ERRORLEVEL%
But %WINSCP_RESULT% is always 0 even if the log remains "Script: Exit code: 1".
I want to get the real error code.
Please advise.
Thank you.

err.png

Reply with quote

Advertisement

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

Re: %ERRORLEVEL% is always 0

Your script looks good. There's probably some tiny syntax problem that you post does not show.
Check this: https://winscp.net/eng/docs/faq_batch_file#newline_escaping

What do you get if you put ECHO ERRORLEVEL %ERRORLEVEL% right after the "exit" line?

This is wrong: "ls "%FTP_DIR_LIVE%"" it should be:
    "ls ""%FTP_DIR_LIVE%""" ^
What do you get if you try this?
winscp.com /command "foo"
echo %ERRORLEVEL%

Reply with quote

CZ
Guest

%ERRORLEVEL% always returns 0 even there is an error from the WinSCP.com script

I am calling the WinSCP.com /script="%sftp_common_script%" from Windows batch script (my.cmd).
I tested few failed scenarios, but it always return 0 for %ERRORLEVEL%. Does anyone know what is wrong with my code? Thank you!

In the my.cmd file:
WinSCP.com /ini=nul /console /script="%sftp_common_script%" /parameter "%sftp_user%" "%sftp_server%" "%sftp_ssh_key%" "%sftp_host_key%" "%%f" "./%au_printer%/%%~nf%%~xf"
IF %ERRORLEVEL% equ 0 (
    ECHO %ERRORLEVEL%
    exit /b 0
) ELSE (
    ECHO %ERRORLEVEL%
    exit /b 1
)
In the %sftp_common_script% file:
option batch abort
option confirm off
open sftp://%1%@%2%/ -privatekey="%3%"  -hostkey="%4%"
put  -resumesupport=off -transfer=b -nopermissions -nopreservetime %5% %6%
close
exit
Result:
C:\Program Files (x86)\WinSCP>C:\SVN\vw_sls_cde\CC\SFTP_Cypress_Print\my.cmd
Printing to DEV/QA Cypress...
..........................................................................
Printing C:\SVN\vw_sls_cde\CC\SFTP_Cypress_Print\SFTP_Cypress_test.pdf...
batch           abort
confirm         off
Searching for host...
Connecting to host...
Authenticating...
Using username "sftp-dlisshcypress".
Authenticating with public key "sftp-dlisshcypress-dev".
Authenticated.
Starting the session...
Session started.
Active session: [1] sftp-dlisshcypress@sftp-dev.vwoa.na.vwg
C:SFTP_Cypress_test.pdf   |            0 B |    0.0 KB/s | binary |   0%
Cannot create remote file '/ftin_Incentive_AUDI2/SFTP_Cypress_test.pdf'.
General failure (server should provide error description).
Error code: 4
Error message from server: Folder not found: /ftin_Incentive_AUDI2/SFTP_Cypress_test.pdf
Common reasons for the Error code 4 are:
- Renaming a file to a name of already existing file.
- Creating a directory that already exists.
- Moving a remote file to a different filesystem (HDD).
- Uploading a file to a full filesystem (HDD).
- Exceeding a user disk quota.
(A)bort, (R)etry, (S)kip, Ski(p) all: Abort
ERRORLEVEL 0

Reply with quote

CZ
Guest

Re: %ERRORLEVEL% always returns 0 even there is an error from the WinSCP.com script

CZ: Yes, I am getting the files from a local directory using for loop.

Reply with quote

Advertisement

Guest

Re: %ERRORLEVEL% always returns 0 even there is an error from the WinSCP.com script

CZ: Hi, I found out that When checking the %ERRORLEVEL% value in the FOR LOOP in the Windows batch script, its value will not change until end of FOR LOOP. I have to call a subroutine outside of the FOR LOOP to see the change of the %ERRORLEVEL%. My issue has been resolved. Thanks for pointing out the FOR LOOP in your reply.

Reply with quote

martin
Site Admin
martin avatar

Re: %ERRORLEVEL% always returns 0 even there is an error from the WinSCP.com script

It looks like you didn't read the link I have posted.
It shows you much better solution with setlocal EnableDelayedExpansion and !errorlevel!.

Reply with quote

Advertisement

You can post new topics in this forum