as it throws error if I serach in the wrong directory and the execution stops, I cannot try in all the listed directories
Shouldn't be the case. You can always cd to directory and check for return codes of FTP list file command. It will tell you whether file exists or not. It won't terminate your program or your FTP session.
Bottomline, you'll have to cd to each directory and look for the file.