move readDir to after the replacement value is set

in order to avoid a situation where the search is performed but
replacement is incorrect