How to find whether a particular command has failed inside an sftp script?

hi,

how can i know whether a command inside an sftp script has failed or not?

i have a sftp expect script

#!/usr/bin/expect
spawn /usr/bin/sftp abc@ftp.abc.com
expect "sftp>"
send "cd dir\r"
expect "sftp>"
send "mput abc.txt\r"
expect "sftp>"
send "mput def.xls\r"
expect "sftp>"
send "bye\r"
expect eof

suppose there is no directory named "dir" in sftp remote server, then the command at line 4 i.e. cd dir will fail and the script should not proceed further.
how can i do this?

expect {
  "550 dir: No such file or directory" {send "quit\n" ; exit }
  "250 CWD command successful" {exp_continue}
}

where should i add this code block? and how it works??

Right after the

send "cd dir\r"

You probably have to adjust the "550 .." and "250 CWD..." to your servers response as well.

This works like case/switch. You could add even more "cases".

I am not getting how to adjust the return codes. can you give me an example how to do that.. modify the above sample script given by me.

Not a trick question; simply change the text to match the exact text your sftp server sends when you do those commands manually...