Topic "If statement not working properly when STAT command executes"

Author Message
Guest




Hi

I am using below code to check the whether file transferred properly and file exist by using STAT command. Infact the if the STAT command fails it should go to a case statement ERROR and execute the content of that case statement. But it is not reading the error case statement properly.

(
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

) > C:\Users\a160559\Desktop\Stat_out_exec_1.log

> Infact the above code is not going to case statement error when the file doesnt exist or the file transfer fails. Is there some other way to catch the Error Code: 2 to some variable.
> I would like to know whats exact the exit 0 and exit 1 does in this code. I tried the below code too.

(
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 (
echo Error or file /data/dsc_files/COMP_UPDATE.log not exists
goto end
)
file /data/dsc_files/COMP_UPDATE.log exists
goto end
:end
echo finished
) > C:\Users\a160559\Desktop\Stat_out_exec_3.log

output was :
Can't get attributes of file '/data/dsc_files/COMP_UPDATE.log'.
No such file or directory.
Error code: 2
Error message from server: No such file
Request code: 7

Here it is not catching the exception at all.

It would be great if someone look into the code.
Advertisements
Guest




Hi

Can I get an quick reply regarding this issue.

Thanks
SHIYAS M
Guest




It would be great if someone reply to this issue, so that I can close the development as soon as possible.

Thanks
SHIYAS M
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24512
Location: Prague, Czechia
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:

Code:
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:

Code:
(
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

_________________
Martin Prikryl
Guest




Thanks a lot. It worked.

I am having a point to clarify. Whats the difference between exit 0 and exit 1. From googling I found that exit 0 will totaly come out of the code. But what is exit 1.

Thanks
SHIYAS M
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24512
Location: Prague, Czechia
Quote:
Whats the difference between exit 0 and exit 1. From googling I found that exit 0 will totaly come out of the code. But what is exit 1.

The exit command stops the script. If you specify an exit code as its argument, it returns that code to a parent process. An exit without an argument is equivalent to an exit 0.
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License