Fix undefined var

Hi,
Just tried sync-exec and got an error: `tmp` var is undefined.
So I replaced it with `dir` and added a regexp to remove trailing slash.