fix inDirectory to reset to the original directory in case an exception ...

...occurs
close #1673