If statement not working properly when STAT command executes

Advertisement

Guest

If statement not working properly when STAT command executes

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.

Reply with quote

Advertisement

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

Reply with quote

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

Re: If statement not working properly when STAT command executes

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:

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:

(
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

Reply with quote

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

Reply with quote

Advertisement

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

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.

Reply with quote

Advertisement

You can post new topics in this forum