If you do a search of these forums on sftp you will find many postings on this subject. You can only use sftp in interactive mode. For scripting you might try using expect.
Another option would be if you are getting the entire contents of the directory, like in your example, is just to do an scp -r on that directory. Then you would get it all and it a one line command and easily scriptable. Also better to set up pub/priv keys for SSH too instead of putting the user name and password in the script.