rewrite script for handling path and dir